1

I have a hash map in which Date is stored as a key and array list is a value. I want to sort the map in order to display a latest date first and old date last.

E.g we have 4 dates as a key like "01-09-2014","02-09-2014","31-08-2014","30-08-2014";

So output should be "02-09-2014","01-09-2014","31-08-2014", "30-08-2014"

Please help me to solve this issue.

Thanks.

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47
gaurav_376
  • 59
  • 2
  • 6
  • Hi welcome! On the verge of sounding rude, you must start with writing some code and then post the *issues* that arise :) – Suvarna Pattayil Sep 08 '14 at 12:04
  • possible duplicate of [How to sort HashMap based on Date?](http://stackoverflow.com/questions/8298290/how-to-sort-hashmap-based-on-date) – Raedwald Sep 08 '14 at 12:31

3 Answers3

2

Try with this

public static void main(String[] args) {
        Map<Date, Integer> m = new HashMap<Date, Integer>();

        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

        try {
            m.put(dateFormat.parse("31-05-2011").getTime(), 67);
            m.put(dateFormat.parse("01-06-2011").getTime(), 89);
            m.put(dateFormat.parse("10-06-2011").getTime(), 56);
            m.put(dateFormat.parse("25-05-2011").getTime(), 34);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Map<Date, Integer> m1 = new TreeMap(m, new Comparator<Date>() {
          @Override
          public int compareTo(Date a, Date b) {
            return -a.compare(b);
          }
        });
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        for (Map.Entry<Date, Integer> entry : m1.entrySet()) {
            System.out.println(df.format(entry.getKey()));
        }
    }
NoDataFound
  • 11,381
  • 33
  • 59
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • that why he use a TreeMap in m1. But it should override the Comparator (Date sort in natural order, not reverse order). – NoDataFound Sep 08 '14 at 12:14
  • @NoDataFound This `new TreeMap(m)`, has `Date` as a key, also `Date` class overrides `compareTo` method. – Ankur Singhal Sep 08 '14 at 12:17
  • @assylias This new `TreeMap(m)`, has `Date` as a key, also `Date` class overrides `compareTo` method – Ankur Singhal Sep 08 '14 at 12:17
  • @NoDataFound just to clear you comments, please refer [this](http://stackoverflow.com/questions/8298290/how-to-sort-hashmap-based-on-date) – Ankur Singhal Sep 08 '14 at 12:20
  • @NoDataFound can i have upvote now instead of downvote – Ankur Singhal Sep 08 '14 at 12:21
  • @ankur-singhal: I commented the fact it use TreeMap (which is correct), but the question ask for reverse ordering ("I want to sort the map in order to display a latest date first and old date last.). That's why I said it. – NoDataFound Sep 08 '14 at 12:30
1

Use TreeMap and define an appropriate Comparator that reverse thing (eg: (Date a, Date b) -> -a.compare(b));

NoDataFound
  • 11,381
  • 33
  • 59
1
static void sortMap() throws Exception{
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    Map<Date, Integer> map = new TreeMap<Date, Integer>(new Comparator<Date>() {
        public int compare(Date date1, Date date2) {
            return date2.compareTo(date1);
        }
    });

    map.put(dateFormat.parse("01-09-2014"), 1);
    map.put(dateFormat.parse("02-09-2014"), 2);
    map.put(dateFormat.parse("31-08-2014"), 3);
    map.put(dateFormat.parse("30-08-2014"), 4);
}
Vipul Paralikar
  • 1,508
  • 10
  • 22