0

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
------------------------------------
Community
  • 1
  • 1
Alex
  • 27
  • 1
  • 6

2 Answers2

4

You can use a custom Comparator like this.

String data = "PTRRRRRR     08-20-2011\n" +
        "TESTDESC     08-01-2012\n" +
        "TESTMYO      12-29-2011\n" +
        "TESTTEST     08-01-2012\n" +
        "TESTPROL     08-01-2012\n" +
        "TESTPRO      08-01-2012\n" +
        "TESTPRO      08-01-2012\n" +
        "PNAMELQ      08-02-2012\n" +
        "TESTMYO      12-29-2011\n";
Map<String, String> map = new LinkedHashMap<String, String>();
for (String line : data.split("\n")) {
    String[] keyValue = line.split(" +");
    map.put(keyValue[0], keyValue[1]);
}
Map.Entry<String, String>[] entries = map.entrySet().toArray(new Map.Entry[map.size()]);
Arrays.sort(entries, new Comparator<Map.Entry<String, String>>() {
    SimpleDateFormat date = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
        try {
            return date.parse(o1.getValue()).compareTo(date.parse(o2.getValue());
        } catch (ParseException e) {
            throw new AssertionError(e);
        }
    }
});
for (Map.Entry entry : entries) {
    System.out.println(entry);
}

prints

PTRRRRRR=08-20-2011
TESTMYO=12-29-2011
TESTDESC=08-01-2012
TESTTEST=08-01-2012
TESTPROL=08-01-2012
TESTPRO=08-01-2012
PNAMELQ=08-02-2012
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

First check http://docs.oracle.com/javase/6/docs/api/java/util/Map.html and notice, that there are differnt Map Implementations:

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

In http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html you'll find this definition:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Since you want to compare by value (Map<String, Date>), you need to implement a Comparator that compares by value (this is untested):

class ValueComparator implements Comparator<String> {

    Map<String, Date> map;
    public ValueComparator(Map<String, Date> map) {
        this.map = map;
    }

    public int compare(String a, String b) {
        return map.get(a).compareTo(map.get(b));
    }
}

(You may want to build the ValueComparator more generic.)

Finally construct the map with

Map<String, Date> sorted = new TreeMap<String, Date>(new ValueComparator(unsortedMap));
sorted.putAll(unsortedMap);
steffen
  • 16,138
  • 4
  • 42
  • 81