package me.konsolas.aac.util.general; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class MapUtil { public static > Map sortByValueDescending(Map map) { List> entries = new LinkedList<>(map.entrySet()); Collections.sort(entries, new DescendingValueComparator<>()); Map sorted = new LinkedHashMap<>(); for (Entry entry : entries) { sorted.put(entry.getKey(), entry.getValue()); } return sorted; } private static final class DescendingValueComparator> implements Comparator> { @Override public int compare(Entry first, Entry second) { return second.getValue().compareTo(first.getValue()); } } }