Java Utililty Methods HashMap Sort

List of utility methods to do HashMap Sort

Description

The list of methods to do HashMap Sort are organized into topic(s).

Method

Collection>getSortedColors(HashMap colors)
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;
...
ListgetSortedHashMapKeyset(Map sorting)
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;
...
LinkedHashMapgetSortedLinkedHashMap(Map bitCounts, Comparator 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;
HashMapsortByComparator(HashMap unsortMap, final boolean ascendingorder)
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;
HashMapsortByValue(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();) {
...
LinkedHashMapsortedScoreMap(HashMap unSortedMap)
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>();
...
ArrayListsortHashMapByKeys(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> passedMap, String key0)
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);
...
LinkedHashMapsortHashMapByValues(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();
...
LinkedHashMapsortHashMapByValues(HashMap passedMap)
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;
});
...