• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a Map By Value  RSS feed

 
Mike Dawson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to sort a map by values. I know how to use a Comparator to do this with a treemap. Only problem is it wont allow duplicates. Any ideas??

Thanks

Mike
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know a solution to this problem, but there is no real reason behind sorting a Map by its values. Could you please explain why is this sorting required in your program?
 
Mike Dawson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Im doing a printout of the key value pairing and the requirement is now to sort it descending by value
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy it into a TreeSet<Map.Entry<K,V>> with a Comparator<Map.Entry<K,V>> instead:

you should also include your keys in the comparison in case the values are the same, otherwise you'll lose several keys.
 
Mike Dawson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob but this TreeMap wont allow duplicate Values will it (ie when doing the comparing)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think Rob meant to use Sets rather than Maps (at least his first sentence suggests that)
SortedSet<Map.Entry<K,V>> entries = new TreeSet<Map.Entry<K,V>>(comparator);
[ October 28, 2008: Message edited by: Joanne Neal ]
 
Mike Dawson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies I kinda solved it used the sql to order the ResultSet then put that into a LinkedHashMap to preserve order then did my processing
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne is right of course. I meant SortedSet and TreeSet. You can tell by the "addAll" call - Map only has putAll.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a Set won't allow duplicates, you may be better off using a List, and sort it using Collections.sort() using a Comparator.
[ October 28, 2008: Message edited by: Mike Simmons ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the combination of value + key can never lead to duplicates, since duplicate keys are not possible in maps. So should the values of two entries be equal, the keys most definitely will not.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I should have elaborated a bit more. The requirement was merely to to sort by value. Sorting by value and then by key was an elaboration on that requirement, that may or may not be necessary, or desirable. Using Collections.sort() on a List bypasses this issue. I do like the idea of sorting by both key and value, and would probably do that myself in most cases. But I thought a simpler alternative might be in order in this case.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in the end both can have the (almost) same result, with just as many lines of code.

With a SortedSet you first create the TreeSet with comparator first, then add all elements.
With a List you create the List with the entrySet() first, then sort it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking of the fact the comparator needs to be a bit more complex for the TreeSet solution.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just slightly.

For List this is enough:

For Set you need to add another comparison in case the values are equal:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!