Array.sort(function)
Posted: 25 Aug 2010, 08:59
Hi guys. I've already found on the forums that the extensions don't support the array.sortOn method but they support the array.sort(). The problem is that to sort my array, i use a function but in the end, it sorts alphabetically.
Here's my code:
And if i trace anything in the orderScore, it traces correctly and I've already checked that it founds correctly that one score is bigger than the other.
Does anyone knows why it doesn't sort? Thanks in advance.
Here's my code:
Code: Select all
function updateLeaderBoard(who){
response = {};
var sortList = [];
for (var i in leaderBoardUsers){
var user = leaderBoardUsers[i];
uObj = {};
uObj.name = user.getName();
uObj.uId = user.getUserId();
uObj.mod = user.isModerator();
uObj.score = user.getVariable("score").getValue() * 1;
sortList.push(uObj)
}
sortList.sort(orderScore)
}
function orderScore(user1, user2){
if (user1.score < user2.score) {
return 1;
} else if (user1.score > user2.score) {
return -1;
} else {
return 0;
}
}
And if i trace anything in the orderScore, it traces correctly and I've already checked that it founds correctly that one score is bigger than the other.
Does anyone knows why it doesn't sort? Thanks in advance.