Java Map Reverse sortByValue(Map map, boolean reverse)

Here you can find the source of sortByValue(Map map, boolean reverse)

Description

sort By Value

License

Open Source License

Declaration

public static Map sortByValue(Map map, boolean reverse) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static Map sortByValue(Map map, boolean reverse) {
        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {
            @Override//from w w w  .  j  av  a  2s.  co  m
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        if (reverse) {
            Collections.reverse(list);
        }

        Map result = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
}

Related

  1. reverseMapping(Map value2keys, Map map)
  2. reverseMapping(Map map)
  3. sort(Map> dataNodes, boolean reverse)
  4. sort(Map source, boolean reverse)
  5. sortByUsedSpace(Map> dataNodes, boolean reverse)
  6. sortByValue(Map map, boolean doReverse)
  7. toReverseIndex( Map> forwardIndex)