Here you can find the source of sortByValue(Map
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> -((o1.getValue()).compareTo(o2.getValue()))); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); }/*from w ww .ja v a2s . c om*/ return result; } }