Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort map keys by values.
public static <T, S extends Comparable<? super S>> List<T> valueSortedKeys(final Map<T, S> map)
{
List<T> list = new ArrayList<T>(map.keySet());
Collections.sort(list, new Comparator<T>()
{
public int compare(T t1, T t2)
{
return map.get(t1).compareTo(map.get(t2));
}
});
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment