Java Map Sort sortByValues(final Map map)

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

Description

sort By Values

License

Apache License

Declaration

private static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) 

Method Source Code


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

import java.util.*;

public class Main {
    private static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
        Comparator<K> valueComparator = new Comparator<K>() {
            public int compare(K k1, K k2) {
                int compare = map.get(k2).compareTo(map.get(k1));
                if (compare == 0)
                    return 1;
                else
                    return compare;
            }//from  www  .  j a v a  2 s. com
        };
        Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
        sortedByValues.putAll(map);
        return sortedByValues;
    }
}

Related

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