2

我正在尝试根据对象中的字段 [marks] 对包含对象的数组进行排序。该数组在 Firefox 和 chrome 中的排序方式不同。在 Chrome 和 IE9 中,第十六项无缘无故地移到了数组的顶部。由于所有标记都是 0,我假设不会进行排序。在 Firefox 中没有排序发生,但在 chrome 和 IE 中不是这样。请在下面找到我使用过的代码。任何关于为什么会发生这种情况的建议将不胜感激

var arr = [
{"userid":"1","name":"USER_1","subjectid":"123","marks":"0"},
{"userid":"2","name":"USER_2","subjectid":"123","marks":"0"},
{"userid":"3","name":"USER_3","subjectid":"123","marks":"0"},
{"userid":"4","name":"USER_4","subjectid":"123","marks":"0"},
{"userid":"5","name":"USER_5","subjectid":"123","marks":"0"},
{"userid":"6","name":"USER_6 ","subjectid":"123","marks":"0"},
{"userid":"7","name":"USER_7","subjectid":"123","marks":"0"},
{"userid":"8","name":"USER_8","subjectid":"123","marks":"0"},
{"userid":"9","name":"USER_9","subjectid":"123","marks":"0"},
{"userid":"10","name":"USER_10","subjectid":"123","marks":"0"},
{"userid":"11","name":"USER_11","subjectid":"123","marks":"0"},
{"userid":"12","name":"USER_12","subjectid":"123","marks":"0"},
{"userid":"13","name":"USER_13","subjectid":"123","marks":"0"},
{"userid":"14","name":"USER_14","subjectid":"123","marks":"0"},
{"userid":"15","name":"USER_15","subjectid":"123","marks":"0"},
{"userid":"16","name":"USER_6","subjectid":"123","marks":"0"},
{"userid":"17","name":"USER_17","subjectid":"123","marks":"0"},
{"userid":"18","name":"USER_8","subjectid":"123","marks":"0"},
{"userid":"19","name":"USER_19","subjectid":"123","marks":"0"},
{"userid":"20","name":"USER_20","subjectid":"123","marks":"0"},
{"userid":"21","name":"USER_21","subjectid":"123","marks":"0"},
{"userid":"22","name":"USER_22","subjectid":"123","marks":"0"},
{"userid":"23","name":"USER_23","subjectid":"123","marks":"0"},
{"userid":"24","name":"USER_24","subjectid":"123","marks":"0"},
{"userid":"25","name":"USER_25","subjectid":"123","marks":"0"},
{"userid":"26","name":"USER_26","subjectid":"123","marks":"0"},
{"userid":"27","name":"USER_27","subjectid":"123","marks":"0"},
{"userid":"28","name":"USER_28","subjectid":"123","marks":"0"},
{"userid":"29","name":"USER_29","subjectid":"123","marks":"0"},
{"userid":"30","name":"USER_30","subjectid":"123","marks":"0"},
{"userid":"31","name":"USER_31","subjectid":"123","marks":"0"},
{"userid":"32","name":"USER_32","subjectid":"123","marks":"0"},
{"userid":"33","name":"USER_33","subjectid":"123","marks":"0"}
];

console.log(JSON.stringify(arr));

arr.sort(function(a,b){
    return b['marks'] - a['marks'];
});

for(var i = 0; i < arr.length; i++){
        arr[i].position = i + 1;
}


console.log(JSON.stringify(arr));​
4

0 回答 0