Page 1 of 1

Array.sort(function)

Posted: 25 Aug 2010, 08:59
by rjgtav
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:

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.

Posted: 28 Aug 2010, 03:55
by mistermind
Humm from the looks of it you are using AS1 Extensions right? Well AS1 is actually just Javascript. A quick search on google and I found this:
http://www.kirupa.com/forum/showthread.php?t=325522
(It seems someone was also missing the sortOn on JS)

and this:
http://www.javascriptkit.com/javatutors/arraysort.shtml

Basically, on your future google searches, use "Javascript" as your keyword to find language related solutions. I also use AS1 extensions and this is how I always find the missing links =D

Hope that helps!

Posted: 28 Aug 2010, 10:45
by rjgtav
Hi. sorry, i forgot to tell that it started working for no reason.