Javascript: sort objects

Posted by tom on Stack Overflow See other posts from Stack Overflow or by tom
Published on 2010-06-01T06:59:54Z Indexed on 2010/06/01 7:03 UTC
Read the original article Hit count: 216

Filed under:
|
function Player() {
  var score;

  this.getScore = function() { return score; }
  this.setScore = function(sc) { score = sc; }
}

function compare(playerA, playerB) {
  return playerA.getScore() - playerB.getScore();
}

var players = [];

players['player1'] = new Player();
players['player2'] = new Player();

Array(players).sort(compare);

I have code that is similar to the above. When I step through the code with a debugger, the compare function never gets called and the array isn't sorted. I'm not sure what's wrong with my code?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about sorting