Skip to content

Instantly share code, notes, and snippets.

@saginadir
Created January 9, 2017 16:40
Show Gist options
  • Save saginadir/5fad0d0ece124cc1975693fed15f5e05 to your computer and use it in GitHub Desktop.
Save saginadir/5fad0d0ece124cc1975693fed15f5e05 to your computer and use it in GitHub Desktop.
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