This question already has an answer here:
这个问题在这里已有答案:
- How to sort an array of objects by multiple fields? 22 answers
如何按多个字段对对象数组进行排序? 22个答案
Can someone help me with Array.sort()? I can do sorting on one value (e.g. return a < b
) but not with more values. I need this case, when some primary values has the same result.
有人可以帮我解决Array.sort()吗?我可以对一个值进行排序(例如,返回 )但不能使用更多值。我需要这种情况,当一些主要值具有相同的结果时。
E.g. array:
var data = [
{
"name": "Paolos Pizza",
"rating_count": 20,
"rating_value": 5,
"price": 7
},
{
"name": "Bella Italia",
"rating_count": 55,
"rating_value": 3,
"price": 7
},
{
"name": "Mama Mia",
"rating_count": 2,
"rating_value": 5,
"price": 99
},
{
"name": "Mario's" ,
"rating_count": 23,
"rating_value": 6,
"price": 7
},
{
"name": "Colazione e Roma" ,
"rating_count": 52,
"rating_value": 4,
"price": 7
}
];
First I want to sort the Array descending on the key price
. If some entries have the same price, than I want to sort it ascending depending on rating_value
. If some entries have the same price
and the same rating_value
, than I want to sort ascending on rating_count
.
首先,我想根据关键价格对Array进行排序。如果某些条目具有相同的价格,那么我希望根据rating_value对其进行升序排序。如果某些条目具有相同的价格和相同的rating_value,那么我想在rating_count上按升序排序。
How can I solve this?
我怎么解决这个问题?
2 个解决方案
#1
1
You can chain the comparison parts with logical OR ||
and take the difference as result for the sort order callback.
您可以使用逻辑OR ||链接比较部分并将差异作为排序顺序回调的结果。
var array = [{ "name": "Paolos Pizza", "rating_count": 20, "rating_value": 5, "price": 7 }, { "name": "Bella Italia", "rating_count": 55, "rating_value": 3, "price": 7 }, { "name": "Mama Mia", "rating_count": 2, "rating_value": 5, "price": 99 }, { "name": "Mario's", "rating_count": 23, "rating_value": 6, "price": 7 }, { "name": "Colazione e Roma", "rating_count": 52, "rating_value": 4, "price": 7 }];
array.sort(function (a, b) {
return b.price - a.price ||
a.rating_value - b.rating_value ||
a.rating_count - b.rating_count;
});
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
#2
1
This is your solution:
这是你的解决方案:
arr.sort(function(a,b) {
if(a.price != b.price) return a.price < b.price;
if(a.rating_value != b.rating_value) return a.rating_value < b.rating_value;
return a.rating_count < b.rating_count;
});
You can make sorting direction accordingly..
你可以相应地做出排序方向。
#1
1
You can chain the comparison parts with logical OR ||
and take the difference as result for the sort order callback.
您可以使用逻辑OR ||链接比较部分并将差异作为排序顺序回调的结果。
var array = [{ "name": "Paolos Pizza", "rating_count": 20, "rating_value": 5, "price": 7 }, { "name": "Bella Italia", "rating_count": 55, "rating_value": 3, "price": 7 }, { "name": "Mama Mia", "rating_count": 2, "rating_value": 5, "price": 99 }, { "name": "Mario's", "rating_count": 23, "rating_value": 6, "price": 7 }, { "name": "Colazione e Roma", "rating_count": 52, "rating_value": 4, "price": 7 }];
array.sort(function (a, b) {
return b.price - a.price ||
a.rating_value - b.rating_value ||
a.rating_count - b.rating_count;
});
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
#2
1
This is your solution:
这是你的解决方案:
arr.sort(function(a,b) {
if(a.price != b.price) return a.price < b.price;
if(a.rating_value != b.rating_value) return a.rating_value < b.rating_value;
return a.rating_count < b.rating_count;
});
You can make sorting direction accordingly..
你可以相应地做出排序方向。