Java Map Sort sortByComparatorDecreasing(Map unsortMap)

Here you can find the source of sortByComparatorDecreasing(Map unsortMap)

Description

sort By Comparator Decreasing

License

Apache License

Declaration

public static Map<String, Integer> sortByComparatorDecreasing(Map<String, Integer> unsortMap) 

Method Source Code

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

import java.util.*;

public class Main {
    public static Map<String, Integer> sortByComparatorDecreasing(Map<String, Integer> unsortMap) {

        // Convert Map to List
        List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

        // Sort list with comparator, to compare the Map values
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o2, Map.Entry<String, Integer> o1) {
                return (o1.getValue()).compareTo(o2.getValue());
            }//from  w  ww .  jav a  2  s  . c om
        });

        // Convert sorted map back to a Map
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
            Map.Entry<String, Integer> entry = it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }
}

Related

  1. sort(Map src)
  2. sort(T map, Comparator> c)
  3. sortBy(Collection collection, final Map valuesMap)
  4. sortByComparator(Map unsortMap)
  5. sortByComparator(Map unsortMap)
  6. sortByComparatorDouble( final Map map)
  7. sortByDescendingValues(Map unsortMap)
  8. sortByKey(final Map map, String order)
  9. sortByKey(Map map)