Java Utililty Methods Map Sort

List of utility methods to do Map Sort

Description

The list of methods to do Map Sort are organized into topic(s).

Method

MapsortByValue(Map map)
sort By Value
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());
return result;
MapsortByValue(Map map)
sort By Value
return sortByValue(map, false);
MapsortByValue(Map map)
Sort a map by value (increasing order)
return sortByValue(map, new Comparator<Entry<K, V>>() {
    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        return (o1.getValue()).compareTo(o2.getValue());
});
MapsortByValue(Map map)
sort By Value
List<Map.Entry<K, V>> entriesInList = extractEntriesSortedByValue(map);
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : entriesInList) {
    result.put(entry.getKey(), entry.getValue());
return result;
MapsortByValue(Map map)
Sort the value in the map and swap the pair of view and its rank value in descending order.
return sortByValue(map, true);
MapsortByValue(Map map, boolean descending)
Sorts a map by its values.
int s2 = 1;
if (descending)
    s2 = -1;
final int sign = s2;
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
...
LinkedHashMapsortByValue(Map map, final boolean asc)
Sorts map by value (http://stackoverflow.com/a/2581754)
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        if (asc) {
            return (o1.getValue()).compareTo(o2.getValue());
        } else {
            return (o2.getValue()).compareTo(o1.getValue());
...
MapsortByValue(Map map)
Sort a map by its values
List<Entry<S, T>> list = new LinkedList<Entry<S, T>>(map.entrySet());
Collections.sort(list, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
Map<S, T> result = new LinkedHashMap<S, T>();
for (Entry<S, T> entry : list) {
    result.put(entry.getKey(), entry.getValue());
return result;
MapsortByValue(Map map)
sort By Value
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
});
Map<String, String> result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
...
MapsortByValueAscending(Map map)
sort By Value Ascending
return sortByValue(map, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));