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

MapsortMap(Map map, Comparator> compator)
sort Map
Map<K, V> result = new LinkedHashMap<K, V>();
List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet());
Collections.sort(entries, compator);
for (Entry<K, V> entry : entries) {
    result.put(entry.getKey(), entry.getValue());
return result;
MapsortMapByKey(Map data)
sort Map By Key
Map<K, V> data_ = new LinkedHashMap<>();
List<K> list = new LinkedList<>(data.keySet());
Collections.sort(list);
for (K k : list) {
    data_.put(k, data.get(k));
return data_;
List>>sortMapByKey(Map map)
sort Map By Key
if (map == null) {
    return null;
List<Set<Entry<String, Object>>> returnlist = new LinkedList(map.entrySet());
Collections.sort(returnlist, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey());
});
return returnlist;
MapsortMapByValue(Map input, final boolean desc)
sort Map By Value
LinkedHashMap<K, V> output = new LinkedHashMap<K, V>(input.size());
ArrayList<Map.Entry<K, V>> entryList = new ArrayList<Map.Entry<K, V>>(input.size());
Collections.sort(entryList, new Comparator<Map.Entry<K, V>>() {
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        if (desc)
            return o2.getValue().compareTo(o1.getValue());
        return o1.getValue().compareTo(o2.getValue());
});
for (Map.Entry<K, V> entry : entryList) {
    output.put(entry.getKey(), entry.getValue());
return output;
MapsortMapByValue(Map map, Comparator comparator)
Sort a Map by value and save it as a linkedHashMap to ensure the order is kept.
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;
MapsortMapByValue(Map map, Comparator> valueComparator)
Creates a new sorted map, based on the values from the given map and Comparator.
if (map == null) {
    return Collections.emptyMap();
List<Entry<K, V>> entriesList = new LinkedList<>(map.entrySet());
Collections.sort(entriesList, valueComparator);
Map<K, V> orderedMap = new LinkedHashMap<>(entriesList.size());
for (Entry<K, V> entry : entriesList) {
    orderedMap.put(entry.getKey(), entry.getValue());
...
MapsortMapByValue(Map map, Comparator> comparator)
Sorts given map by given comparator
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
list.sort(comparator);
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
    result.put(entry.getKey(), entry.getValue());
return result;
LinkedHashMapsortMapByValue(Map map, final boolean descending)
sort Map By Value
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        int comp = (o1.getValue()).compareTo(o2.getValue());
        if (descending) {
            comp = comp * (-1);
        return comp;
...
List>sortMapByValue(Map oriMap)
sort Map By Value
if (oriMap == null || oriMap.isEmpty()) {
    return null;
List<Map.Entry<String, Integer>> entries = new ArrayList<>(oriMap.entrySet());
Collections.sort(entries, (o1, o2) -> o2.getValue() - o1.getValue());
return entries;
MapsortMapByValue(Map unsortMap, boolean highFirst)
sort Map By Value
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
    @Override
    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
        return (o1.getValue()).compareTo(o2.getValue());
});
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
...