Created
January 9, 2017 16:40
-
-
Save saginadir/5fad0d0ece124cc1975693fed15f5e05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const players = {itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201}; | |
const newPlayers = []; | |
for (var player in players) { | |
if (players.hasOwnProperty(player)) { | |
newPlayers.push({ | |
player, | |
rank: players[player], | |
}); | |
} | |
} | |
// newPlayers now contains array of objects { player: string, rank: number } | |
// We now need to sort by desc rank. | |
// But wait.. how do you sort in vanilla JS? | |
// fullstackoverflow development to the rescue! | |
// http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects | |
const sortedPlayers = newPlayers.sort(function(a, b) { | |
return parseFloat(b.rank) - parseFloat(a.rank); | |
}); | |
console.log(sortedPlayers); // Done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment