/** * */ package nicetext; import java.util.Comparator; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; /** * @author vikasing */ public class Utils { public <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> entriesSortedByValues(Map<K, V> map) { SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>( new Comparator<Map.Entry<K, V>>() { @Override public int compare(Map.Entry<K, V> e2, Map.Entry<K, V> e1) { int res = e1.getValue().compareTo(e2.getValue()); return res != 0 ? res : 1; } } ); sortedEntries.addAll(map.entrySet()); return sortedEntries; } }