Sorting a HashMap of Doubles from highest to lowest.

Discussion in 'Spigot Plugin Development' started by NickMD, Jun 1, 2016.

Thread Status:
Not open for further replies.
  1. Heres the problem, I put every online player in a hashmap
    Code (Text):
    HashMap<String, Double> map = new HashMap<String, Double>();
    for(Player p : Bukkit.getOnlinePlayers())
      map.put(p.getName(), economy.getBalance(p.getName()));
    Im trying to order them and get the top 10 balances.
     
  2. You need to sort them manually using a sorting algorithm. There is no built in way to do it, unless there is one in Apache I don't know about. I'll write you a quick way to do it, give me a minute.
     
  3. You can use Collections for that.
     
  4. I would instead create a TreeMap and initialize it with a comparator (You could even use the comparator from the previous guy's answer) and then use map.addAll(..) to add the data into the map. The output will be a lovely sorted map! woo!
     
    • Agree Agree x 2
    • Like Like x 1
    • Winner Winner x 1
  5. TreeMaps sort by Key, not by Value. He wants things sorted by Value. HUGE difference between the two.
     
    • Agree Agree x 1
  6. It seems that the code you've given me sorts the Doubles from lowest to highest. I was needing highest to lowest, I now need help to change the order of the map.
     
  7. I'm not surprised: it sorts by the total ordering of Double.
     
  8. And this is exactly the problem with copying and pasting the code in without reading it and understanding it. This is exactly the problem with spoon feeding. You were given code and you used it unconditionally without properly understanding what it does. And now here you are again asking for more code. There are a million and one ways to tackle the problem that you are presented with here, but none of the solutions are worth anything to you if you don't sit down and try to work one out for yourself.
     
  9. The code you've been given is sufficient enough for you to be able to rearrange it, also why is this in your profile?

     
    • Agree Agree x 3
    • Like Like x 1
    • Winner Winner x 1
  10. Then alter the comparator so it sorts by value high->low? That is why I said initialize it with a comparator haha.
     
  11. I actually found the solution to this thanks any ways
     
  12. I love how you deleted the message:
    "Bitch I actually did work on the problem and found the solution an hour after your post just because you talk shit doesn't make you a problem solver"
    (Obviously, I couldn't quote that because you deleted it, but I feel like the person who you directed it at (@Silent ), and those who want to hire you for being a "Professional" Java "Developer" would see how wrong they could actually be).
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.