Java Map Sort sortByValueDescending( Map map)

Here you can find the source of sortByValueDescending( Map map)

Description

Sorts a Map by value in descending order.

License

Open Source License

Parameter

Parameter Description
map a map that should be sorted.

Return

a sorted map

Declaration

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(
        Map<K, V> map) 

Method Source Code

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

Related

  1. sortByValue(Map map, final boolean asc)
  2. sortByValue(Map map)
  3. sortByValue(Map map)
  4. sortByValueAscending(Map map)
  5. sortByValueDESC(Map map)
  6. sortByValues( Map map)
  7. sortByValues(final Map map)
  8. sortByValues(Map map, final Comparator comp)
  9. sortByValues(Map map)