我有一个对象数组,每个对象都有一个名为sort_order
的属性,我需要根据另一个属性score
进行更改但是捕获的是我实际上无法更改数组的顺序表示没有Array.sort()
。
基本上我想要这个:
var scores = [{id:int, name:string, score:int, sort_order:int}, ...];
function compare(a,b){
return b.score - a.score;
}
scores.sort(compare);
但我没有实际排序,而是相应地设置a.sort_order
和b.sort_order
。
答案 0 :(得分:0)
您可以通过对不同的数组进行排序来对它们创建排序顺序。最简单的解决方案是对数组的副本进行排序:
scores.slice().sort(compare).forEach(function(o, i) {
o.sort_order = i;
});