# @codingame/thenby 中文文档

规范地址：https://npmcn.com/package/%40codingame/thenby/

版本：1\.0\.0
npm 元数据更新时间：2025-07-15T14:03:55.382Z
中文译文更新时间：2026-11-10T09:14:51.059Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40codingame/thenby
原文对照：https://npmcn.com/package/%40codingame/thenby/?lang=original
项目主页：https://github.com/CodinGame/thenBy.js
源码仓库：https://github.com/CodinGame/thenBy.js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：13.6 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2024-09-27T09:50:56.162Z
- 最新版本发布：2024-09-27T09:50:56.446Z
- 近 30 天下载量：2031（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

使用第一个By (.thenBy (.thenBy) 语法排序数组的微库

## 安装

```sh
npm install @codingame/thenby
```

也可以使用：`pnpm add @codingame/thenby`、`yarn add @codingame/thenby`、`bun add @codingame/thenby`

## README

-   [然后By.js 习惯](https://npmcn.com/package/%40codingame/thenby/#readme-thenbyjs-usage)
    -   [按属性名称排序](https://npmcn.com/package/%40codingame/thenby/#readme-sort-by-property-names)
    -   [以空函数排序](https://npmcn.com/package/%40codingame/thenby/#readme-sort-by-unary-functions)
    -   [额外选项](https://npmcn.com/package/%40codingame/thenby/#readme-extra-options)
        -   [排序下降](https://npmcn.com/package/%40codingame/thenby/#readme-sort-descending)
        -   [大小写不敏感排序](https://npmcn.com/package/%40codingame/thenby/#readme-case-insensitive-sorting)
        -   [自定义比较函数](https://npmcn.com/package/%40codingame/thenby/#readme-custom-compare-function)
        -   [国际化:使用土著的Javascripts `Intl.Collator`](https://npmcn.com/package/%40codingame/thenby/#readme-internationalization-using-javascripts-native-intlcollator)
    -   [说说表现](https://npmcn.com/package/%40codingame/thenby/#readme-a-word-on-performance)
    -   [安装](https://npmcn.com/package/%40codingame/thenby/#readme-installing)
        -   [在您的 HTML 中安装](https://npmcn.com/package/%40codingame/thenby/#readme-install-in-your-html)
        -   [使用 npm 或 纱线安装](https://npmcn.com/package/%40codingame/thenby/#readme-install-using-npm-or-yarn)

# 然后By.js 习惯

[![NPM Version](https://img.shields.io/npm/v/@codingame/thenby.svg)](https://npmjs.org/package/@codingame/thenby) [![NPM Downloads](https://img.shields.io/npm/dm/@codingame/thenby.svg)](https://npmjs.org/package/@codingame/thenby)

`thenBy` 是一个 Javascript 微库, 帮助排序多个密钥上的数组 。 它允许你使用 [本地矩阵: sort () 方法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) ,但通过多个函数来排序 `firstBy().thenBy().thenBy()` 样式.

示例

```javascript
// first by length of name, then by population, then by ID
data.sort(
    firstBy(function (v1, v2) { return v1.name.length - v2.name.length; })
    .thenBy(function (v1, v2) { return v1.population - v2.population; })
    .thenBy(function (v1, v2) { return v1.id - v2.id; })
);
```

`thenBy` 也提供了一些很好的捷径,使得最常用的排序方式更加容易更可读.

### 按属性名称排序

Javascripting 严重依赖传递的歧义函数返回 -1, 0 或 1 对项目. 虽然这非常灵活,但您往往想要排序一个简单的属性的价值. 为了方便,By.js为您构建了相应的比较功能,如果您以属性名(而不是函数)通过的话. 上面的例子就像这样:

```javascript
// first by length of name, then by population, then by ID
data.sort(
    firstBy(function (v1, v2) { return v1.name.length - v2.name.length; })
    .thenBy("population")
    .thenBy("id")
);
```

如果元素没有定义属性,它会排序为空字符串 (""). 通常,这是在顶部.

### 以空函数排序

您也可以通过一个函数,该函数取出单个项目并返回其排序键。 这使上述表达式变为:

```javascript
// first by length of name, then by population, then by ID
data.sort(
    firstBy(function (v) { return v.name.length; })
    .thenBy("population")
    .thenBy("id")
);
```

注意:javascript包含一些标准功能,也可以在此传递. Number () 函数将使您在数值上排序排序, 而不是词汇值 :

```javascript
var values = ["2", "20", "03", "-2", "0", 200, "2"];
var sorted = values.sort(firstBy(Number));
```

## 额外选项

### 排序下降

然后By.js允许您在第二个参数中通过 `direction`。如果通过“ desc” 或 -1 ,则排序将被倒转。 所以说:

```javascript
// first by length of name descending, then by population descending, then by ID ascending
data.sort(
    firstBy(function (v1, v2) { return v1.name.length - v2.name.length; }, -1)
    .thenBy("population", "desc")
    .thenBy("id")
);
```

### 大小写不敏感排序

(截至v1.2.0) (中文(简体) ). 所有快捷方式都允许您对案件不敏感进行排序。 第二个参数预期一个选项对象(如果是一个数字,则解释为 `direction` (见上文)。 忽略 Case 属性可以设定为真实, 像这样 :

```javascript
// first by name, case insensitive, then by population
data.sort(
    firstBy("name", {ignoreCase:true})
    .thenBy("population")
);
```

如果您想要同时使用降入和忽略 Case, 您也必须使用选项语法来表达方向 :

```javascript
// sort by name, case insensitive and descending
data.sort(firstBy("name", {ignoreCase:true, direction:"desc"}));
```

### 自定义比较函数

如果您对精确的排序顺序有更具体的愿望,但仍想利用unary函数的方便或者在属性名上进行排序,您可以在选项中通过自己的比较函数. 在这里,我们使用一个比较函数,该函数知道打牌的相对值:

```javascript
const cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
var cardCompare = (c1, c2) =>{
    return cards.indexOf(c1) - cards.indexOf(c2);
}
var handOfCards =  [
        { id: 7, suit:"c", card:"A" },
        { id: 8, suit:"d", card:"10" },
        // etc
    ];
handOfCards.sort(firstBy("card", {cmp: cardCompare, direction: "desc"}));

```

你可以用这个 `cmp` 函数与 `direction`,但不与 `ignoreCase` (明显原因).

### 国际化:使用土著的Javascripts `Intl.Collator`

比较您可能想要传递的功能的更有趣的习惯之一是本地 `compare` 由 `Intl.Collator`。此比较函数知道不同文化中不同的排序规则。 许多浏览器已经实施了这些,但在NodeJS中,API得到了实施,但只针对英语文化. 你会用它,然后B 像这样:

```javascript
// in German, ä sorts with a
var germanCompare = new Intl.Collator('de').compare;
// in Swedish, ä sorts after z
var swedishCompare = new Intl.Collator('sv').compare;
data.sort(
    firstBy("name", {cmp: swedishCompare})
);
```

检查 [关于使用Intl的详情。 整理器](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator)。 。 。 .

## 说说表现

然后为您构建一个比较器函数。 它通过将您传递的功能与一些小的公用功能合并来做到这一点,这些功能执行"还原","将当前排序顺序与上一个排序相合并"等任务. 另外,这些操作试图正确工作,无论排序的阵列中的内容是什么. 这里有两个步骤需要花费时间:构建über功能并运行. 建造时间应始终是可忽略不计的。 不过,运行时间可能比小心地手动比较功能要慢。 不过, _一般你不必担心这个_但是如果你整理的非常大, 它可能很重要。 例如,在使几个小函数互相调用而不是创建一个代码方面有一些间接费用. 而且,如果你很清楚你的数据, 并且知道一个特定的领域是 _永远存在_ 已经 _总是一个数字_,你可以编码一个显著的更快的 比较函数 然后By的结果。 单位测试包含一个极端的例子.

如果使用时By将多个比较函数合并为一个函数(其中每个函数期望两个参数),相差会很小. 使用unary函数会增加一些间接费用,使用方向:desc会增加一些,只使用属性名会增加一点,但会检查缺失值,可以优化. 被忽略的案件会减慢,但不会超过手编码时.

## 安装

### 使用 npm 或 纱线安装

`npm install @codingame/thenby`

或者说

`yarn add @codingame/thenby`

然后在您的应用程序中:

```typescript
var firstBy = require('@codingame/thenby');
```

或类型脚本/ES6:

```typescript
import { firstBy } from "@codingame/thenby";
```

## 常见问题

### @codingame/thenby 是什么？

使用第一个By \(\.thenBy \(\.thenBy\) 语法排序数组的微库

### 如何安装 @codingame/thenby？

在项目目录运行 npm install @codingame/thenby，或使用 pnpm add / yarn add / bun add。

### @codingame/thenby 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @codingame/thenby 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @codingame/thenby 可以商用吗？

@codingame/thenby 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @codingame/thenby 的下载量有多少？

根据 npm 官方下载统计，@codingame/thenby 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,031 次，日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @codingame/thenby 还在维护吗？

最新版本 1\.0\.0 发布于 2024/09/27（2 年前）；至今共发布 1 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/09/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

