List of utility methods to do Map Sort
Map | sortMap(Map sort Map Map<K, V> result = new LinkedHashMap<K, V>(); List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet()); Collections.sort(entries, compator); for (Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); return result; |
Map | sortMapByKey(Map sort Map By Key Map<K, V> data_ = new LinkedHashMap<>(); List<K> list = new LinkedList<>(data.keySet()); Collections.sort(list); for (K k : list) { data_.put(k, data.get(k)); return data_; |
List | sortMapByKey(Map sort Map By Key if (map == null) { return null; List<Set<Entry<String, Object>>> returnlist = new LinkedList(map.entrySet()); Collections.sort(returnlist, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey()); }); return returnlist; |
Map | sortMapByValue(Map sort Map By Value LinkedHashMap<K, V> output = new LinkedHashMap<K, V>(input.size()); ArrayList<Map.Entry<K, V>> entryList = new ArrayList<Map.Entry<K, V>>(input.size()); Collections.sort(entryList, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { if (desc) return o2.getValue().compareTo(o1.getValue()); return o1.getValue().compareTo(o2.getValue()); }); for (Map.Entry<K, V> entry : entryList) { output.put(entry.getKey(), entry.getValue()); return output; |
Map | sortMapByValue(Map Sort a Map by value and save it as a linkedHashMap to ensure the order is kept. List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, comparator); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); return result; |
Map | sortMapByValue(Map Creates a new sorted map, based on the values from the given map and Comparator. if (map == null) { return Collections.emptyMap(); List<Entry<K, V>> entriesList = new LinkedList<>(map.entrySet()); Collections.sort(entriesList, valueComparator); Map<K, V> orderedMap = new LinkedHashMap<>(entriesList.size()); for (Entry<K, V> entry : entriesList) { orderedMap.put(entry.getKey(), entry.getValue()); ... |
Map | sortMapByValue(Map Sorts given map by given comparator List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); list.sort(comparator); Map<K, V> result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); return result; |
LinkedHashMap | sortMapByValue(Map sort Map 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 comp = (o1.getValue()).compareTo(o2.getValue()); if (descending) { comp = comp * (-1); return comp; ... |
List | sortMapByValue(Map sort Map By Value if (oriMap == null || oriMap.isEmpty()) { return null; List<Map.Entry<String, Integer>> entries = new ArrayList<>(oriMap.entrySet()); Collections.sort(entries, (o1, o2) -> o2.getValue() - o1.getValue()); return entries; |
Map | sortMapByValue(Map sort Map By Value List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); }); Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); ... |