Java Map Sort sortMapByValuesDescending( Map aMap)

Here you can find the source of sortMapByValuesDescending( Map aMap)

Description

Why the fuck do neither Java nor Guava have a Function to do this?

License

LGPL

Declaration

public static <K, V extends Comparable> LinkedHashMap<K, V> sortMapByValuesDescending(
        Map<K, V> aMap) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;
import java.util.Map.Entry;

public class Main {
    /**//ww w .jav a2  s . c om
     * Why the fuck do neither Java nor Guava have a Function to do this?
     */
    public static <K, V extends Comparable> LinkedHashMap<K, V> sortMapByValuesDescending(
            Map<K, V> aMap) {
        List<Map.Entry<K, V>> tEntrySet = new LinkedList<Map.Entry<K, V>>(
                aMap.entrySet());
        Collections.sort(tEntrySet, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Entry<K, V> aValue1, Entry<K, V> aValue2) {
                return -aValue1.getValue().compareTo(aValue2.getValue());
            }
        });
        LinkedHashMap<K, V> rMap = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> tEntry : tEntrySet)
            rMap.put(tEntry.getKey(), tEntry.getValue());
        return rMap;
    }
}

Related

  1. sortMapByValue(Map unsortMap, boolean highFirst)
  2. sortMapByValue(Map oriMap)
  3. sortMapByValue(Map oriMap, final boolean isDesc)
  4. sortMapByValues(Map map, final boolean inverted)
  5. sortMapByValuesDecreasing( final Map mapToSort)
  6. sortMapDesc(Map map)
  7. sortMapDescByValue(Map map)
  8. sortMapDescendingByValue(Map unsortedMap)
  9. sortMapUsingComparator(final Map map, final Comparator comparator)