Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Created January 13, 2015 10:12
Show Gist options
  • Save d3ep4k/4969519b62a0d5894a7d to your computer and use it in GitHub Desktop.
Save d3ep4k/4969519b62a0d5894a7d to your computer and use it in GitHub Desktop.
Java/Groovy - Sorting Map on Value
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