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.