Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?
I have a map in java with Map<String, String>.Map contaions <Id, Date>. i want to sort these map according to date. how can i do that? This is the map after print it
-------------------------------------
PTRRRRRR 08-20-2011
-------------------------------------
TESTDESC 08-01-2012
-------------------------------------
TESTMYO 12-29-2011
-------------------------------------
TESTTEST 08-01-2012
-------------------------------------
TESTPROL 08-01-2012
-------------------------------------
TESTPRO 08-01-2012
-------------------------------------
PNAMELQ 08-02-2012
-------------------------------------
TESTMYO 12-29-2011
------------------------------------
First check http://docs.oracle.com/javase/6/docs/api/java/util/Map.html and notice, that there are differnt
MapImplementations:In http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html you’ll find this definition:
Since you want to compare by value (
Map<String, Date>), you need to implement aComparatorthat compares by value (this is untested):(You may want to build the ValueComparator more generic.)
Finally construct the map with