You must compare them inside the sort function . If the function returns a negative value, a goes to b (in ascending order), if it is positive, b goes to a. If the return value is 0, they are equal:
temp.sort(function(a, b) {
if (a.rank < b.rank) {
return -1;
} else if (a.rank > b.rank) {
return 1;
} else {
return 0;
}
});
, , :
temp.sort((a, b) {
return a.rank - b.rank;
});
:
temp.sort((a, b) {
return b.rank - a.rank;
});
ES6:
temp.sort((a, b) => b.rank - a.rank;