Java Map Sort sortByValueAscending(Map map)

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

Description

sort By Value Ascending

License

Apache License

Declaration

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

Method Source Code


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

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAscending(Map<K, V> map) {
        return sortByValue(map, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
    }/*from   ww w . jav  a 2s.c om*/

    private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map,
            Comparator<Map.Entry<K, V>> comparator) {

        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
        Collections.sort(list, comparator);

        Map<K, V> result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }
}

Related

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