Here you can find the source of sortByValueDescending( Map
Parameter | Description |
---|---|
map | a map that should be sorted. |
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending( Map<K, V> map)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.util.*; public class Main { /**/* ww w . j a v a 2 s . c o m*/ * Sorts a Map by value in descending order. * * @param map a map that should be sorted. * @return a sorted map */ public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending( Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); Map<K, V> result = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; } }