List of utility methods to do Map Sort
Map | sortByDescendingValues(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();) { ... |
Map | sortByKey(final Map 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) { ... |
HashMap | sortByKey(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; |
T | sortByKey(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)); }); |
Map | sortByKeys(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); |
Map | sortByKeys(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 sort By Map return lists;
|
List | sortByMap(Map 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 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 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; ... |