Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

javascript - Array.sort working differently in chrome and firefox and IE

I'm trying to sort an array that contains objects, based on the field[marks] in the object. The array is getting sorted differently in Firefox and chrome. In Chrome and IE9 the sixteenth item gets shifted to the top of the array for no reason. Since all the marks are 0 I assumed no sorting will take place. No sorting happens in Firefox but that's not the case with chrome and IE. Please find the code below I have used. Any advice to why this happens would be much appreciated

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));?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...