例如,我要针对进行排序Intl.Collator().compare。有什么方法可以传递此比较器以供使用_.sortBy?
您可以使用 lodash mixin
_.mixin({
sortWith : function(arr, customFn) {
return _.map(arr).sort(customFn)
}
});
Run Code Online (Sandbox Code Playgroud)
你现在可以做
_.sortWith(array, function(a, b) {
//custom function that returns either -1, 0, or 1 if a is <, ==, or > than b
});
Run Code Online (Sandbox Code Playgroud)
你现在可以像这样链接:
_.chain(myObject)
.get('some_array_property')
.sortWith(function(a, b) {
//determine if a <=> b
})
.value();
Run Code Online (Sandbox Code Playgroud)
在内部, sortWith 将数组映射到一个新数组,这样它就不会修改传递给它的数组并使用本机 sort()方法。
不,不幸的是,目前这是不可能的。
解决方法是使用该iteratees函数将值映射到标准比较器将正确排序的内容。然而这几乎不切实际。
这里也有人要求https://github.com/lodash/lodash/issues/246,但作者没有回复。
| 归档时间: |
|
| 查看次数: |
4533 次 |
| 最近记录: |