Java Map Sort sortByValuesDesc(final Map map)

Here you can find the source of sortByValuesDesc(final Map map)

Description

sort By Values Desc

License

Apache License

Declaration

public static <K, V extends Comparable<V>> Map.Entry<K, V>[] sortByValuesDesc(final Map<K, V> map) 

Method Source Code

//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;
    }
}

Related

  1. sortByValues(Map map, final Comparator comp)
  2. sortByValues(Map map)
  3. sortByValues(Map map)
  4. sortByValues(Map map)
  5. sortByValues(Map map)
  6. sortClustersKeys(final Map> clusters)
  7. sortDoubleMap(Map map)
  8. sortedByRankThenLength(Map map)
  9. sortedKeys(Map map)