Java Utililty Methods Map Sort

List of utility methods to do Map Sort

Description

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

Method

LinkedHashMapsort(Map map)
sort
LinkedHashMap<K, V> returnMap = new LinkedHashMap<K, V>();
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
for (K key : keys) {
    returnMap.put(key, map.get(key));
return returnMap;
Mapsort(Map map, Comparator> comparator)
sort
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, comparator);
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
    result.put(entry.getKey(), entry.getValue());
return result;
Mapsort(Map map)
sort
Map<String, Object> sorted = new LinkedHashMap<String, Object>();
List<String> keys = new ArrayList<String>(map.size());
keys.addAll(map.keySet());
Collections.sort(keys);
for (String key : keys) {
    sorted.put(key, map.get(key));
return sorted;
...
Mapsort(Map src)
sort
Map<String, Object> result = new LinkedHashMap<String, Object>();
List<String> keys = new ArrayList<>(src.keySet());
Collections.sort(keys);
for (String key : keys) {
    result.put(key, src.get(key));
return result;
Tsort(T map, Comparator> c)
sort
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, c);
Object result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
    ((T) result).put(entry.getKey(), entry.getValue());
return (T) result;
CollectionsortBy(Collection collection, final Map valuesMap)
sort By
List<T> ordered = new ArrayList<T>(collection);
Comparator<T> compartor = new Comparator<T>() {
    @SuppressWarnings("unchecked")
    @Override
    public int compare(T a, T b) {
        C aValue = valuesMap.get(a);
        C bValue = valuesMap.get(b);
        return ((Comparable<C>) aValue).compareTo(bValue);
...
MapsortByComparator(Map unsortMap)
sort Map by key desc
List list = new LinkedList(unsortMap.entrySet());
Collections.sort(list, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey());
});
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
...
MapsortByComparator(Map unsortMap)
sort By Comparator
List<Entry<Integer, Integer>> list = new ArrayList<Entry<Integer, Integer>>(unsortMap.entrySet());
Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {
    @Override
    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
        return o1.getValue().compareTo(o2.getValue());
});
Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>();
...
MapsortByComparatorDecreasing(Map unsortMap)
sort By Comparator Decreasing
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
    public int compare(Map.Entry<String, Integer> o2, Map.Entry<String, Integer> o1) {
        return (o1.getValue()).compareTo(o2.getValue());
});
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
...
MapsortByComparatorDouble(final Map map)
Descending order HashMap by value
List<Entry<String, Double>> map_arr = new LinkedList<Entry<String, Double>>(map.entrySet());
Collections.sort(map_arr, new Comparator<Entry<String, Double>>() {
    public int compare(Entry<String, Double> v1, Entry<String, Double> v2) {
        return v2.getValue().compareTo(v1.getValue());
});
LinkedHashMap<String, Double> sortedByComparator = new LinkedHashMap<String, Double>();
for (Entry<String, Double> e : map_arr) {
...