-1

I've read about sorting list alphabetically such as: Hashmap sorting, Sorting Maps

I have a Map<String, Integer> with values such as

Tarantulas, 6 
Lions, 5 
Snakes, 2 
Zoopies, 2 
Zappin, 2 
Chapas, 1 
Zong Zwing, 1 
Chingos, 1 
Chapis, 1 
Grouches, 0 

I need to (ONLY) sort the sections that have the same points alphabetically. This is sample data so will not know the actual values in the Map therefore need to sort based on whatever values are present. I have already grouped/sorted by values using:

 public <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
        Comparator<K> valueComparator = new Comparator<K>() {
            public int compare(K k1, K k2) {
                int compare = map.get(k2).compareTo(map.get(k1));
                if (compare == 0) {
                    return 1;
                } else {
                    return compare;
                }
            }
        };
        Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
        sortedByValues.putAll(map);
        return sortedByValues;
    }

This is the expected result:

Tarantulas, 6 
Lions, 5 
Snakes, 2 
Zappin, 2 
Zoopies, 2 
Chapas, 1 
Chapis, 1 
Chingos, 1 
Zong Zwing, 1 
Grouches, 0 

So question is: How do I sort only those sections of the Map that have the same points (Integer values), and leave the rest as is?

I'm using Java 7.

Community
  • 1
  • 1
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
  • Is the use of a map mandatory? I would use a set of objects instead. – SimoV8 Jan 16 '16 at 08:13
  • I used a comparator. Will add that code to question although I donn't think it's possibly useful. You can see that the Map is sorted according to value and that Integer values in the Map are grouped together. – Afshin Ghazi Jan 16 '16 at 08:13
  • 1
    You should not use a Map. Use a List, where Animal is a class with a name and a value. Sort it with a Comparator that sorts by value, then sorts by name. – JB Nizet Jan 16 '16 at 08:16
  • My comparator is returning a Map. Perhaps I can get another container or convert the Map to something else if you think it would be easier. Thanks – Afshin Ghazi Jan 16 '16 at 08:16

2 Answers2

2

Here is a example of how to use a comparator. After sorting you could put all the entries from the set into a LinkedHashMap to preserve the order of the elements.

public static void main(String[] args) {

    Map<String,Integer> map = new HashMap<String, Integer>();

    map.put("Tarantulas", 6);
    map.put("Lions", 5);
    map.put("Snakes", 2);
    map.put("Zoopies", 2);
    map.put("Zappin", 2);
    map.put("Chapas", 1);
    map.put("Zong Zwing", 1);
    map.put("Chingos", 1);
    map.put("Chapis", 1);
    map.put("Grouches", 0);

    SortedSet<Map.Entry<String, Integer>> sortedSet = new TreeSet<>(new Comparator<Map.Entry<String,Integer>>() {
        @Override
        public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {

            int result = o1.getValue().compareTo(o2.getValue());
            result*=-1;
            if(result==0)
                result = o1.getKey().compareTo(o2.getKey());

            return result;
        }
    });

    sortedSet.addAll(map.entrySet());


    for(Entry<String, Integer> entry:sortedSet)
        System.out.println(entry.getKey()+"="+entry.getValue());

}
1

Try:

public <K  extends Comparable<K>, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator = new Comparator<K>() {
        public int compare(K k1, K k2) {
            int compare = map.get(k2).compareTo(map.get(k1));
            if (compare == 0) {
                return k1.compareTo(k2); // <- To sort alphabetically
            } else {
                return compare;
            }
        }
    };
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
  • This couples the old map, the new map and the entries in it all together, making the whole thing practically read-only. If any changes are done, consistency errors will arise. – Henry Jan 16 '16 at 08:32