然后By.js 习惯
thenBy 是一个 Javascript 微库, 帮助排序多个密钥上的数组 。 它允许你使用 本地矩阵: sort () 方法 ,但通过多个函数来排序 firstBy().thenBy().thenBy() 样式.
示例
// 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为您构建了相应的比较功能,如果您以属性名(而不是函数)通过的话. 上面的例子就像这样:
// 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")
);
如果元素没有定义属性,它会排序为空字符串 (""). 通常,这是在顶部.
以空函数排序
您也可以通过一个函数,该函数取出单个项目并返回其排序键。 这使上述表达式变为:
// 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 () 函数将使您在数值上排序排序, 而不是词汇值 :
var values = ["2", "20", "03", "-2", "0", 200, "2"];
var sorted = values.sort(firstBy(Number));
额外选项
排序下降
然后By.js允许您在第二个参数中通过 direction。如果通过“ desc” 或 -1 ,则排序将被倒转。 所以说:
// 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 属性可以设定为真实, 像这样 :
// first by name, case insensitive, then by population
data.sort(
firstBy("name", {ignoreCase:true})
.thenBy("population")
);
如果您想要同时使用降入和忽略 Case, 您也必须使用选项语法来表达方向 :
// sort by name, case insensitive and descending
data.sort(firstBy("name", {ignoreCase:true, direction:"desc"}));
自定义比较函数
如果您对精确的排序顺序有更具体的愿望,但仍想利用unary函数的方便或者在属性名上进行排序,您可以在选项中通过自己的比较函数. 在这里,我们使用一个比较函数,该函数知道打牌的相对值:
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 像这样:
// 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的详情。 整理器。 。 。 .
说说表现
然后为您构建一个比较器函数。 它通过将您传递的功能与一些小的公用功能合并来做到这一点,这些功能执行"还原","将当前排序顺序与上一个排序相合并"等任务. 另外,这些操作试图正确工作,无论排序的阵列中的内容是什么. 这里有两个步骤需要花费时间:构建über功能并运行. 建造时间应始终是可忽略不计的。 不过,运行时间可能比小心地手动比较功能要慢。 不过, 一般你不必担心这个但是如果你整理的非常大, 它可能很重要。 例如,在使几个小函数互相调用而不是创建一个代码方面有一些间接费用. 而且,如果你很清楚你的数据, 并且知道一个特定的领域是 永远存在 已经 总是一个数字,你可以编码一个显著的更快的 比较函数 然后By的结果。 单位测试包含一个极端的例子.
如果使用时By将多个比较函数合并为一个函数(其中每个函数期望两个参数),相差会很小. 使用unary函数会增加一些间接费用,使用方向:desc会增加一些,只使用属性名会增加一点,但会检查缺失值,可以优化. 被忽略的案件会减慢,但不会超过手编码时.
安装
使用 npm 或 纱线安装
npm install @codingame/thenby
或者说
yarn add @codingame/thenby
然后在您的应用程序中:
var firstBy = require('@codingame/thenby');
或类型脚本/ES6:
import { firstBy } from "@codingame/thenby";