Just a short one in Javascript
Edit: somehow I hadn't noticed the position bit 😳, so I've added that as a map call.
position
ranked = players => players .slice() .sort((a, b) => Math.sign(b.points - a.points) * 2 + a.name.localeCompare(b.name)) .map((item, index, array) => Object.assign(item, { position: index && item.points == array[index - 1].points ? array[index - 1].position : index + 1, }) ); players = [ {name: 'b', points: 1}, {name: 'b', points: 3}, {name: 'b', points: 2}, {name: 'a', points: 2}, {name: 'c', points: 2} ]; console.log(JSON.stringify(ranked(players),null,2)); /* ^ [ { "name": "b", "points": 3, "position": 1 }, { "name": "a", "points": 2, "position": 2 }, { "name": "b", "points": 2, "position": 2 }, { "name": "c", "points": 2, "position": 2 }, { "name": "b", "points": 1, "position": 5 } ] */
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Just a short one in Javascript
Edit: somehow I hadn't noticed the
position
bit 😳, so I've added that as a map call.