Wednesday, August 11, 2010

HashMap reverse sort

We all know that we can sort a map by using TreeMap.
But what if you have to reverse sort the same map, assuming that the key is an Integer?

Generally people do it by converting the map in some list etc...

Well, there is a much simpler approach.

Multiply your key with -1 :D and the natural order of sorting will get reversed on its own and you can then use your favourite TreeMap for sorting :)

I got this idea form one of my mentors in Glomantra..

No comments:

Post a Comment