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

MapsortByValueDESC(Map map)
sort By Value DESC
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) {
        return (o2.getValue()).compareTo(o1.getValue());
});
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
...
MapsortByValueDescending(Map map)
Sorts a Map by value in descending order.
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, 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());
});
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
...
MapsortByValues(Map map)
sort By Values
List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
    @SuppressWarnings("unchecked")
    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        return o1.getValue().compareTo(o2.getValue());
});
...
MapsortByValues(final Map map)
sort By Values
Comparator<K> valueComparator = new Comparator<K>() {
    public int compare(K k1, K k2) {
        int compare = map.get(k2).compareTo(map.get(k1));
        if (compare == 0)
            return 1;
        else
            return compare;
};
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
MapsortByValues(Map map, final Comparator comp)
sort By Values
List<Map.Entry<A, B>> entries = new LinkedList<Entry<A, B>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<A, B>>() {
    @Override
    public int compare(Entry<A, B> o1, Entry<A, B> o2) {
        return comp.compare(o1.getValue(), o2.getValue());
});
Map<A, B> out = new LinkedHashMap<A, B>();
...
List>sortByValues(Map map)
Returns a list of entries sorted by the values descending.
return sortByValues(map, false);
List>sortByValues(Map map)
sort map entries by value an return them in a sorted list
List<Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        return o1.getValue().compareTo(o2.getValue());
});
return entries;
...
MapsortByValues(Map map)
sort By Values
List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        float val1 = Float.parseFloat(o1.getValue().toString());
        float val2 = Float.parseFloat(o2.getValue().toString());
        if (val1 > val2)
            return -1;
...
MapsortByValues(Map map)
sort By Values
List list = new LinkedList<>(map.entrySet());
Collections.sort(list, Map.Entry.comparingByValue().reversed());
HashMap<T, R> sortedHashMap = new LinkedHashMap<>();
for (Iterator it = list.iterator(); it.hasNext();) {
    Map.Entry<T, R> entry = (Map.Entry<T, R>) it.next();
    sortedHashMap.put(entry.getKey(), entry.getValue());
return sortedHashMap;
...
Map.Entry[]sortByValuesDesc(final Map map)
sort By Values Desc
if (map == null || map.size() == 0) {
    return null;
try {
    @SuppressWarnings("unchecked")
    Map.Entry<K, V>[] array = map.entrySet().toArray(new Map.Entry[map.size()]);
    Arrays.sort(array, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
...