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

ListsortClustersKeys(final Map> clusters)
Sort cluster keys by their packages size.
List<String> returnValue;
returnValue = new ArrayList<String>(clusters.keySet());
Collections.sort(returnValue, new Comparator<String>() {
    public int compare(final String first, final String second) {
        return new Integer(clusters.get(first).size()).compareTo(clusters.get(second).size());
});
return returnValue;
...
List>sortDoubleMap(Map map)
sort Double Map
List<Map.Entry<String, Double>> mapSorted = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
Collections.sort(mapSorted, new Comparator<Map.Entry<String, Double>>() {
    public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
        return (o2.getValue()).compareTo(o1.getValue());
});
return mapSorted;
MapsortedByRankThenLength(Map map)
sorted By Rank Then Length
List<Entry<String, int[]>> ll = new LinkedList<Entry<String, int[]>>(map.entrySet());
Collections.sort(ll, new Comparator<Entry<String, int[]>>() {
    @Override
    public int compare(Entry<String, int[]> o1, Entry<String, int[]> o2) {
        int val = Integer.compare(o2.getValue().length, o1.getValue().length);
        if (val == 0)
            val = Integer.compare(o1.getKey().length(), o2.getKey().length());
        return val;
...
SortedSetsortedKeys(Map map)
Returns the keys for the map in a set sorted alphabetically.
return new TreeSet<String>(map.keySet());
MapsortedMap(Map map, Comparator comparator)
Method to sort a map based on the comparator
if (map != null && comparator != null) {
    Map<K, V> returnValue = new HashMap<>();
    List<K> list = new ArrayList<>();
    for (K key : map.keySet()) {
        list.add(key);
    Collections.sort(list, comparator);
    for (int i = 0; i < list.size(); i++) {
...
StringsortedString(Map c)
sorted String
List<T> t = new ArrayList<T>(c.keySet());
Collections.sort(t);
List<V> l = new ArrayList<V>();
List<String> pairs = new ArrayList<String>();
for (T k : t) {
    pairs.add(k + "=" + c.get(k));
return "{" + join(", ", pairs) + "}";
...
StringsortedString(Map c)
sorted String
List<T> t = new ArrayList<T>(c.keySet());
Collections.sort(t);
List<String> pairs = new ArrayList<String>();
for (T k : t) {
    pairs.add(k + "=" + c.get(k));
return "{" + join(", ", pairs) + "}";
MapsortedTable(Map map)
sorted Table
if (map != null) {
    return new TreeMap<String, Object>(map);
} else {
    return new TreeMap<String, Object>();
MapsortedView(Map original, int numItems)
sorted View
List<Map.Entry<String, Long>> values = new LinkedList<Map.Entry<String, Long>>(original.entrySet());
Collections.sort(values, new Comparator<Map.Entry<String, Long>>() {
    public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
        int i = o1.getValue().compareTo(o2.getValue());
        return (-i);
});
Map<String, Long> view = new LinkedHashMap<String, Long>();
...
LinkedHashMapsortEntries(Map map, Comparator> comparator)
sort Entries
LinkedHashMap<K, V> returnMap = new LinkedHashMap<K, V>();
List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet());
Collections.sort(entries, comparator);
for (Entry<K, V> entry : entries) {
    returnMap.put(entry.getKey(), entry.getValue());
return returnMap;