List of utility methods to do Map Sort
Map | sortByValue( Map 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; |
List | sortByValue(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; ... |
List | sortByValue(final 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); ... |
Map | sortByValue(final Map sort By Value return sortByValue(map, true);
|
Map | sortByValue(final Map sort a Map 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>(); ... |
Map | sortByValue(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();) { ... |
Map | sortByValue(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();) { ... |
Map | sortByValue(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; ... |
Map | sortByValue(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); ... |
Map | sortByValue(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()); }); |