Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created April 19, 2015 18:31
Show Gist options
  • Save BasThomas/751d2fff495bd7556d96 to your computer and use it in GitHub Desktop.
Save BasThomas/751d2fff495bd7556d96 to your computer and use it in GitHub Desktop.
Sorting
private static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> unsortMap)
{
List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet());
// Sorting the list based on values
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
{
@Override
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return o1.getValue().compareTo(o2.getValue());
}
});
return list;
}
for (Entry<String, Integer> e : sortByValue(map))
{
output += String.format("%s: %d\n", e.getKey(), e.getValue());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment