Javascript: sort objects

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 similar to the one above. When I view the code using the debugger, the comparison function is never called and the array is not sorted. I'm not sure what happened to my code?

+4
source share
4 answers

It is not sorted because you specified the keys that include the variables in the array. Sorting will only move objects on whole keys. You should see your sorting work if you create your array as follows:

 var players = [new Player(), new Player()]; 

although, of course, it will not be very effective, since you do not have a single account for sorting or a method for identifying them. This will be done:

 function Player(name, score) { this.getName = function() { return name; } this.getScore = function() { return score; } this.setScore = function(sc) { score = sc; } } function comparePlayers(playerA, playerB) { return playerA.getScore() - playerB.getScore(); } var playerA = new Player('Paul', 10); var playerB = new Player('Lucas', 5); var playerC = new Player('William', 7); var players = [playerA, playerB, playerC]; for (var i = 0; i < players.length; i++) alert(players[i].getName() + ' - ' + players[i].getScore()); players.sort(comparePlayers); for (var i = 0; i < players.length; i++) alert(players[i].getName() + ' - ' + players[i].getScore()); 

Hope this helps.

+12
source

The main problem is this line:

Array(players).sort(compare);

Array(something) makes an array with something its element.

 console.log(Array(players)); //[[player1, player2]] 

Use a numeric indexed array instead of using an array like in players['player1']

Run the following code (replace console.log with a warning if you don't have Firebug).

 function Player() { var score; //return this.score - else it returns undefined this.getScore = function() { return this.score; } this.setScore = function(sc) { this.score = sc; } } function compare(playerA, playerB) { console.log("called " + playerA.getScore() + " " + playerB.score); //compare method should return 0 if equal, 1 if a > b and -1 if a < b return (playerA.getScore() == playerB.getScore()) ? 0 : ((playerA.getScore() > playerB.getScore()) ? 1 : -1); } var players = []; players[0] = new Player(); players[1] = new Player(); players[2] = new Player(); players[3] = new Player(); players[0].setScore(9); players[1].score = 14; players[2].score = 11; players[3].score = 10; players.sort(compare); console.log(players);//prints sorted array 
+2
source

Probably because you do not have any "array values" inside your array - text indexes are not considered as array values, but as a property of an object (arrays are "masking objects" in javascript). You can add as many properties to any object, but massive concrete methods such as sort accept only the "real" elements of the array as their parameters (i.e. only with numeric indices)

 var arr = new Array() arr[0] = 1 arr[1] = 2 arr["textual_index"] = 3 alert(arr.length); 

The last line warns "2" not "3", since there are only two values ​​with numeric indices.

0
source

You can also use as below:

 var a = []; a.push(obj1); a.push(obj2); a.sort(compare); 

so that you can use the push method, not the integer index

0
source

Source: https://habr.com/ru/post/1311482/


All Articles