List of utility methods to do Map Sort
Map | sortMapByValue(Map sort Map By Value Map<String, String> sortedMap = new LinkedHashMap<String, String>(); if (oriMap != null && !oriMap.isEmpty()) { List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> entry1, Map.Entry<String, String> entry2) { int value1 = 0, value2 = 0; try { value1 = Integer.parseInt(entry1.getValue()); ... |
Map | sortMapByValue(Map sort Map By Value Map<T, Double> sortedMap = new LinkedHashMap<T, Double>(); if (oriMap != null && !oriMap.isEmpty()) { List<Map.Entry<T, Double>> entryList = new ArrayList<Map.Entry<T, Double>>(oriMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<T, Double>>() { public int compare(Entry<T, Double> entry1, Entry<T, Double> entry2) { Double value1 = entry1.getValue(); Double value2 = entry2.getValue(); if (isDesc) ... |
Map | sortMapByValues(Map Borrowed from http://javarevisited.blogspot.com/2012/12/how-to-sort-hashmap-java-by-key-and-value.html Java method to sort Map in Java by value e.g. 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(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { if (!inverted) return o2.getValue().compareTo(o1.getValue()); else return o1.getValue().compareTo(o2.getValue()); ... |
List | sortMapByValuesDecreasing( final Map sort Map By Values Decreasing List<Map.Entry<K, V>> entries = new ArrayList<>(mapToSort.size()); entries.addAll(mapToSort.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<K, V>>() { @Override public int compare(final Map.Entry<K, V> entry1, final Map.Entry<K, V> entry2) { return entry2.getValue().compareTo(entry1.getValue()); }); ... |
LinkedHashMap | sortMapByValuesDescending(Map Why the fuck do neither Java nor Guava have a Function to do this? List<Map.Entry<K, V>> tEntrySet = new LinkedList<Map.Entry<K, V>>(aMap.entrySet()); Collections.sort(tEntrySet, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Entry<K, V> aValue1, Entry<K, V> aValue2) { return -aValue1.getValue().compareTo(aValue2.getValue()); }); LinkedHashMap<K, V> rMap = new LinkedHashMap<K, V>(); ... |
Map | sortMapDesc(Map sort Map Desc List<Entry<String, Float>> list = new LinkedList<Entry<String, Float>>(map.entrySet()); Collections.sort(list, new Comparator<Entry<String, Float>>() { public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { return o2.getValue().compareTo(o1.getValue()); }); Map<String, Float> sortedMap = new LinkedHashMap<String, Float>(); for (Iterator<Entry<String, Float>> it = list.iterator(); it.hasNext();) { ... |
Map | sortMapDescByValue(Map sort Map Desc By Value List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue().compareTo(o1.getValue()); }); Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Iterator<Entry<String, Integer>> it = list.iterator(); it.hasNext();) { ... |
Map | sortMapDescendingByValue(Map Sorts a map by its values. List<Map.Entry<K, V>> list = new LinkedList<>(unsortedMap.entrySet()); Collections.sort(list, (o1, o2) -> -(o1.getValue()).compareTo(o2.getValue())); Map<K, V> sortedMap = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); return sortedMap; |
Map | sortMapUsingComparator(final Map Sort given map by keys, using specified comparator List<T> operationList = new LinkedList<T>(map.keySet()); Collections.sort(operationList, comparator); Map<T, V> result = new LinkedHashMap<T, V>(); for (T key : operationList) { result.put(key, map.get(key)); return result; |
String | sortParams(Map sort Params List<String> values = new ArrayList<String>(params.values()); Collections.sort(values); String prestr = ""; for (int i = 0; i < values.size(); i++) { String value = values.get(i); prestr = prestr + value; return prestr; ... |