Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
- List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
- Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
- @Override
- public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
- return (e1.getValue()).compareTo(e2.getValue());
- }
- });
- Map<K, V> result = new LinkedHashMap<>();
- for (Map.Entry<K, V> entry : list) {
- result.put(entry.getKey(), entry.getValue());
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement