Array.sort在chrome和firefox以及IE中的工作方式不同

时间:2012-08-06 16:21:22

标签: javascript internet-explorer google-chrome

我试图根据对象中的字段[标记]对包含对象的数组进行排序。该数组在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));​

0 个答案:

没有答案