Sort HashMap<Player,Integer> from highest value to lowest ?

Discussion in 'Spigot Plugin Development' started by Drawethree, Apr 24, 2017.

  1. Hey guys i need help with sorting my map<Player,Integer> where player is player and integer is player's kills. After end of the game, i need to display top 3 players with most kills, but dont know how to sort the map. Could you help me ?
  2. You could get the keySet() from the Map, and loop through all the values and display the 3 highest ints after the loop is done.
  3. Also i need to display TOP 3 Players and their kills, not just kills..
  4. Im totally confused with sorting maps :D
  5. Choco


  6. @2008Choco thank you Choco. And now.. how to get first 3 KeySets from map ?
  7. Having used @2008Choco's example, you would next just need to get the first 3 entries in the LinkedHashMap. You could have some sort of counter variable and a list variable that stores the top 3 players and then begin a for loop going through the "results" LinkedHashMap using #keySet() until you have 3 results. Increment the counter variable and add to your list variable until you have 3. Next, just use the #get() method on the "results" LinkedHashMap to obtain the integer value that corresponds to a player.