Here you can find the source of sortByValuesDesc(final Map
public static <K, V extends Comparable<V>> Map.Entry<K, V>[] sortByValuesDesc(final Map<K, V> map)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static <K, V extends Comparable<V>> Map.Entry<K, V>[] sortByValuesDesc(final Map<K, V> map) { if (map == null || map.size() == 0) { return null; }//w w w .j a v a 2s. c o m try { @SuppressWarnings("unchecked") Map.Entry<K, V>[] array = map.entrySet().toArray(new Map.Entry[map.size()]); Arrays.sort(array, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) { return e2.getValue().compareTo(e1.getValue()); } }); return array; } catch (Exception e) { } return null; } }