I'm looking for a way to hold scores for players for a highscore.
A typical input :
map.put(p1, 9);
map.put(p2, 7);
map.put(p3, 9);
Now I need a way of ordering the map so to be able to see the order of players based on their scores. However, I've tried just sorting based on the value of the map but then I lose cases where two or more players have the same score.
Is a map even the best way to do this or should I try a different data type? All I need is a way to order players based on their scores.
I would keep the score for the player within the Player
object itself. Then you can just create a list of the players, and order it with a Comparator<Player>
implementation that orders by scores - either deliberately making it "highest score first" or using Collections.reverseOrder
to reverse the sort order of a "lower score first" comparator.
See more on this question at Stackoverflow