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

MapsortByDescendingValues(Map unsortMap)
Sort a map by values in descending order keeping the duplicate entries.
List list = new LinkedList(unsortMap.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 sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
...
MapsortByKey(final Map map, String order)
Sort a map by its keys according to a specified order.
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
if (order.compareTo("ascend") == 0) {
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getKey()).compareTo(o2.getKey());
    });
} else if (order.compareTo("descend") == 0) {
...
HashMapsortByKey(Map map)
sort By Key
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
list.sort((a, b) -> {
    return (a.getKey().compareTo(b.getKey()));
});
HashMap<K, V> retMap = new HashMap<K, V>();
for (Entry<K, V> entry : list) {
    retMap.put(entry.getKey(), entry.getValue());
return retMap;
TsortByKey(T map, final boolean descending)
sort By Key
return sort(map, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return ((o1.getKey()).compareTo(o2.getKey()) * (descending ? -1 : 1));
});
MapsortByKeys(Map map)
sort By Keys
Comparator<Map.Entry<K, V>> comparator = new Comparator<Map.Entry<K, V>>() {
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return (o1.getKey()).compareTo(o2.getKey());
};
return sortMapByKeys(map, comparator);
MapsortByKeys(Map map)
sort By Keys
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
Map<K, V> sortedMap = new LinkedHashMap<K, V>();
for (K key : keys) {
    sortedMap.put(key, map.get(key));
return sortedMap;
List>sortByMap(List> lists)
sort By Map
return lists;
ListsortByMap(Map map, int size)
sort By Map
double worstTopScore = Double.MAX_VALUE;
T resultWorstValue = null;
List<T> result = new ArrayList<T>(size);
for (T k : map.keySet()) {
    double score = map.get(k);
    if (result.size() < size) {
        result.add(k);
        if (score < worstTopScore) {
...
List>sortByValue(final Map map)
Sorts a Map by value Partially buggy due to http://stackoverflow.com/questions/109383/how-to-sort -a-mapkey-value-on-the-values-in-java/1283722#1283722
final List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(final Map.Entry<K, V> op1, final Map.Entry<K, V> op2) {
        return (op2.getValue()).compareTo(op1.getValue());
});
return list;
...
List>sortByValue(Map map)
sort By Value
List<Map.Entry<K, V>> sortedEntries = new ArrayList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(sortedEntries, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
        return e2.getValue().compareTo(e1.getValue());
});
return sortedEntries;
...