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, final boolean ascendingValues)
sort 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 value = (ascendingValues ? o1.getValue().compareTo(o2.getValue())
                : o2.getValue().compareTo(o1.getValue()));
        if (value == 0) {
            value = o1.getKey().compareTo(o2.getKey());
        return value;
});
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
    result.put(entry.getKey(), entry.getValue());
return result;
ListsortByValue(final Map m)
sort By Value
List keys = new ArrayList();
keys.addAll(m.keySet());
Collections.sort(keys, new Comparator() {
    public int compare(Object o1, Object o2) {
        Object v1 = m.get(o1);
        Object v2 = m.get(o2);
        if (v1 == null) {
            return (v2 == null) ? 0 : 1;
...
ListsortByValue(final Map map)
sort By Value
List<Integer> list = new ArrayList<Integer>();
list.addAll(map.keySet());
Collections.sort(list, new Comparator<Object>() {
    @SuppressWarnings("unchecked")
    public int compare(Object o1, Object o2) {
        Object v1 = map.get(o1);
        Object v2 = map.get(o2);
        return ((Comparable<Object>) v1).compareTo(v2);
...
MapsortByValue(final Map map)
sort By Value
return sortByValue(map, true);
MapsortByValue(final Map map, final boolean asc)
sort a Map on the values.
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(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return asc ? (o1.getValue()).compareTo(o2.getValue()) : (o2.getValue()).compareTo(o1.getValue());
});
Map<K, V> result = new LinkedHashMap<K, V>();
...
MapsortByValue(Map map)
sort a map by value descending
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
    public int compare(Object o2, Object o1) {
        return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
...
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 result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
...
MapsortByValue(Map map, Comparator aComp)
sort By Value
List list = new LinkedList(map.entrySet());
Collections.sort(list, aComp);
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry) it.next();
    result.put(entry.getKey(), entry.getValue());
return result;
...
MapsortByValue(Map map)
sort By Value
Comparator<Map.Entry<K, V>> valueComparator = null;
valueComparator = new Comparator<Map.Entry<K, V>>() {
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        return o2.getValue().compareTo(o1.getValue());
};
final List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, valueComparator);
...
MapsortByValue(Map map)
sort By Value
return sort(map, new Comparator<Map.Entry<K, V>>() {
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return (o2.getValue()).compareTo(o1.getValue());
});