使用 underscore.js 按值对对象数组进行排序 [英] Sort array of objects by value using underscore.js
问题描述
我正在尝试按 average
属性按降序对对象数组进行排序 - 因此最大的 average
排在第一位 - 但我无法使用下划线.js.以下是我的尝试:
I'm trying to sort an array of objects by the average
property in descending order - so the largest average
is first - but am not able to using underscore.js. Below is my attempt:
var jsonData = [
{
"title": "Dear Kitten",
"totalCount": 1689,
"average": 241
},
{
"title": "Weird Things All Couples Fight About",
"totalCount": 9966,
"average": 1424
},
{
"title": "If Disney Princesses Were Real",
"totalCount": 16567,
"average": 2367
},
{
"title": "Secret Tricks With Everyday Objects",
"totalCount": 24884,
"average": 3555
},
{
"title": "The Coolest Travel Hacks",
"totalCount": 41847,
"average": 8369
},
{
"title": "5 Ways You're Drinking Coffee Wrong",
"totalCount": 55673,
"average": 7953
},
{
"title": "The Perfect Way To Pour A Beer",
"totalCount": 58097,
"average": 58097
},
{
"title": "Fruit You're Eating Wrong",
"totalCount": 65570,
"average": 32785
},
{
"title": "Your Cat Is Judging You",
"totalCount": 78952,
"average": 11279
},
{
"title": "3rd Date vs 30th Date",
"totalCount": 84394,
"average": 14066
}
];
console.log(_.sortBy(jsonData, "average"));
推荐答案
这里的问题是您希望数组按照 average
以降序顺序排序,而不是默认升序.
The issue here is that you wanted the array to be sorted in descending order by average
, instead of the default ascending order.
您可以通过为 _.sortBy()
函数提供自定义 iteratee
来实现:
You could do this by providing a custom iteratee
to the _.sortBy()
function:
_.sortBy( jsonData, function( item ) { return -item.average; } )
但我不建议这样做.简单地使用原生 JavaScript [].sort()
方法并为其提供一个比较函数会更好:
But I don't recommend that. It would be much better to simply use the native JavaScript [].sort()
method and provide it a comparison function:
jsonData.sort( function( a, b ) { return b.average - a.average; } )
如果您对一个非常大的数组进行排序,这也比使用 _.sortBy()
更快.查看 _.sortBy()
的源代码以了解原因:
If you were sorting a very large array, this would also be faster than using _.sortBy()
. Look at the source code for _.sortBy()
to see why:
_.sortBy = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iteratee(value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
除了 .sort()
调用之外,它还做了很多工作——而这段代码只是冰山一角,它调用的辅助函数就像 cb()
也做了很多工作.
It's doing quite a bit of work in addition to the .sort()
call - and this code is just the tip of the iceberg, the helper functions it calls like cb()
do a lot of work too.
当您自己直接调用 .sort()
一样容易时,为什么还要这样做?
Why do all that when it's just as easy to call .sort()
directly yourself?
此外,需要仔细阅读冗长的 .sortBy()
源代码,以确保它执行的是 numeric 排序而不是字典排序 - 并且
Also, it takes a close reading of that lengthy .sortBy()
source to be sure that it does a numeric sort instead of a lexicographic sort - and the documentation doesn't say!
字典排序(又名字母排序)是将值按字符串排序,而不是按数字排序.例如,它将使用以下顺序:
A lexicographic sort (aka alphabetic sort) is where the values are sorted as strings, not as numbers. So for example it would use this order:
[ 1424, 2367, 241, ... ]
当您自己调用原生数组 .sort()
时,您可以轻松验证它是否使用数字排序:值 b.average - a.average
是总是一个数字.
When you call the native array .sort()
yourself, you can easily verify that it uses a numeric sort: the value b.average - a.average
is always a number.
这篇关于使用 underscore.js 按值对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!