我想通过score
值对此对象进行排序,因此我可以填充高分表。找到这种排序方法,它适用于公式中的-
,从最小排序到最高排序可行,但是当我尝试使用+
将其排序时,它不起作用。我很困惑..为什么这个对象数组的排序失败?
JS:
scores = [
{"n":"Arne", "score":700},
{"n":"Bertil", "score":430},
{"n":"Carl", "score":901},
{"n":"David", "score":30},
{"n":"Eric", "score":23},
{"n":"Frida", "score":118},
{"n":"Greg", "score":221},
{"n":"Hulk", "score":543}
];
scores.sort(function(a, b) {
return a.score + b.score;
});
//add rows to table from scores object
var t = document.getElementById('table');
for (var j in scores) {
var r = document.createElement('tr')
r.innerHTML = '<td><span></span></td><td>'+scores[j].n+'</td><td>'+scores[j].s+'</td>'
t.appendChild(r);
}
宾果!谢谢 – jenswirf 2012-02-03 15:37:45