# Sorting a HashMap of Doubles from highest to lowest.

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

Not open for further replies.
1. ### NickMD

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. ### BlizzardFyre

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. ### fuzzlemann

You can use Collections for that.

6. ### storm345

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 x 2
• Like x 1
• Winner x 1
7. ### BlizzardFyre

TreeMaps sort by Key, not by Value. He wants things sorted by Value. HUGE difference between the two.

• Agree x 1
8. ### NickMD

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.

9. ### Spottedleaf

I'm not surprised: it sorts by the total ordering of Double.

10. ### Silent

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.

11. ### v_M_v

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 x 3
• Like x 1
• Winner x 1
12. ### storm345

Then alter the comparator so it sorts by value high->low? That is why I said initialize it with a comparator haha.

13. ### NickMD

I actually found the solution to this thanks any ways

14. ### Msrules123

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 x 1