List of utility methods to do HashMap Sort
Collection | getSortedColors(HashMap get Sorted Colors ArrayList<Entry<Integer, Integer>> colorList = new ArrayList<>(colors.entrySet()); Collections.sort(colorList, new Comparator<Entry<Integer, Integer>>() { @Override public int compare(final Entry<Integer, Integer> o1, final Entry<Integer, Integer> o2) { return -o1.getValue() + o2.getValue(); }); return colorList; ... |
List | getSortedHashMapKeyset(Map get Sorted Hash Map Keyset List<T> types = new ArrayList<>(); Map<T, Integer> clone = new HashMap<>(); for (T player : sorting.keySet()) { clone.put(player, sorting.get(player)); for (int i = 0; i < sorting.size(); i++) { int highestNumber = Integer.MIN_VALUE; T highestType = null; ... |
LinkedHashMap | getSortedLinkedHashMap(Map bitCounts, Comparator get Sorted Linked Hash Map List<Map.Entry> bitCountsList = new LinkedList(bitCounts.entrySet()); Collections.sort(bitCountsList, comparator); LinkedHashMap bitCountsSorted = new LinkedHashMap(); for (Map.Entry entry : bitCountsList) { bitCountsSorted.put(entry.getKey(), entry.getValue()); return bitCountsSorted; |
HashMap | sortByComparator(HashMap sort By Comparator List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet()); Collections.sort(list, new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if (ascendingorder) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); }); HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); return sortedMap; |
HashMap | sortByValue(HashMap 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) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); }); HashMap result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { ... |
LinkedHashMap | sortedScoreMap(HashMap this function takes unsorted map as input and returns a sorted map LinkedList<Entry<Long, Double>> mapList = new LinkedList<Entry<Long, Double>>(unSortedMap.entrySet()); Collections.sort(mapList, new Comparator<Entry<Long, Double>>() { @Override public int compare(Entry<Long, Double> o1, Entry<Long, Double> o2) { return o1.getValue().compareTo(o2.getValue()); }); LinkedHashMap<Long, Double> sortedMap = new LinkedHashMap<Long, Double>(); ... |
ArrayList | sortHashMapByKeys(HashMap passedMap, boolean ascending) sort Hash Map By Keys List mapKeys = new ArrayList(passedMap.keySet()); Collections.sort(mapKeys); if (!ascending) Collections.reverse(mapKeys); ArrayList result = new ArrayList(); Iterator keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { Object key = keyIt.next(); ... |
LinkedHashMap | sortHashMapByValues( HashMap Sort hash map by values key0. List mapKeys = new ArrayList(); List mapValues = new ArrayList(); for (Entry<String, HashMap<String, Object>> me : passedMap.entrySet()) { mapKeys.add(me.getKey()); mapValues.add(me.getValue().get(key0)); Collections.sort(mapValues); Collections.sort(mapKeys); ... |
LinkedHashMap | sortHashMapByValues(HashMap passedMap, boolean ascending) sort Hash Map By Values List mapKeys = new ArrayList(passedMap.keySet()); List mapValues = new ArrayList(passedMap.values()); Collections.sort(mapValues); Collections.sort(mapKeys); if (!ascending) Collections.reverse(mapValues); LinkedHashMap someMap = new LinkedHashMap(); Iterator valueIt = mapValues.iterator(); ... |
LinkedHashMap | sortHashMapByValues(HashMap sort Hash Map By Values List<String> mapKeys = new ArrayList<String>(passedMap.keySet()); List<Integer> mapValues = new ArrayList<Integer>(passedMap.values()); Collections.sort(mapValues, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2) * -1; }); ... |