Reputation: 18800
What I want to do is sort a map by value. I went over many questions that are available on the stackoverflow site and found out following solution that does what I want but missing a small thing.
Link1: Sorting Map
But the issue I am running into is that by default this is sorted by ascending order by value. I want to order it by descending order:
So what I did was I created a class that implements a comparator
class MyComparator implements Comparator {
Map map;
public MyComparator(Map map) {
this.map = map;
}
public int compare(Object o1, Object o2) {
return ((Integer) map.get(o2)).compareTo((Integer) map.get(o1));
}
}
And then I pass my map to the treemap,
MyComparator comp = new MyComparator(myMap);
Map<String, Integer> newMap = new TreeMap(comp);
newMap.putAll(myMap);
This seems like bad approach because I feel this is inefficient. Is there a way to change the solution in the link to do ordering on descending order by default.
Upvotes: 39
Views: 83536
Reputation: 298113
You should use new TreeMap<>(Collections.reverseOrder());
.
Map<String, Integer> newMap = new TreeMap<>(Collections.reverseOrder());
newMap.putAll(myMap);
or to reverse an existing comparator like the value-comparator Collections.reverseOrder(comparator)
. It works like your approach swapping the two objects before invoking compare
/compareTo
.
Upvotes: 135
Reputation: 66
This will work :
TreeMap<Integer, Integer> reverseInteger=new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2>o1?1:o2==o1?0:-1;
}
});
Upvotes: 4
Reputation: 327
You could simply invert the return value of your compare method by adding a minus sign at the beginning:
return -((Integer) map.get(o2)).compareTo((Integer) map.get(o1));
Upvotes: 2
Reputation: 910
TreeMap<Long,String> treeMap = new TreeMap<Long,String>();
NavigableMap <Long, String> nmap = treeMap.descendingMap();
Set<Long, String> set = nmap.entrySet();
Iterator<Long, String> iterator = set.iterator();
now u can iterate over iterator and extract the value using iterator.hasNext() and iterator.next() methods ......
Upvotes: 7
Reputation: 33197
To change the solution in the link to sort by descending order, just reverse the condition:
...
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return 1; // For ascending, return -1;
} else {
return -1; // For ascending, return 1;
} // returning 0 would merge keys
}
...
Upvotes: 0