为什么lodash在数组中反向排序?

时间:2016-11-21 16:17:30

标签: lodash

我使用lodash,以及一些示例代码:

var stuff = [{a: 100}, {a: 90}];

var res1 = _.sortBy(stuff, function(st) {
    return [st.a];
});

var res2 = _.sortBy(stuff, function(st) {
    return st.a;
});



console.log(res1);
console.log(res2);

返回:

[{a: 90}, {a: 100}]
[{a: 100}, {a: 90}]

为什么在返回数组时会切换?

2 个答案:

答案 0 :(得分:4)

排序回调并不期望返回一个数组(为什么会这样?)所以它隐式地将它转换为字符串。字符串按字母顺序排序。在这种特定情况下,9大于1,因此" 90"大于" 100"。

答案 1 :(得分:1)

按多列排序

_.sortBy(data, ['key1', 'key2']);