Created
January 13, 2015 10:12
-
-
Save d3ep4k/4969519b62a0d5894a7d to your computer and use it in GitHub Desktop.
Java/Groovy - Sorting Map on Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map = new LinkedHashMap(); | |
map.put('a', 7); | |
map.put('b', 6); | |
map.put('c', 9); | |
list =new LinkedList<Map.Entry<String, Integer>>( map.entrySet() ); | |
Collections.sort( list, { Map.Entry o1, Map.Entry o2 -> | |
return (o2.getValue()).compareTo( o1.getValue()) | |
}) | |
result = new LinkedHashMap<String, Integer>(); | |
for (Map.Entry entry : list){ | |
result.put( entry.getKey(), entry.getValue() ); | |
} | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment