0

I have two arraylists that consist of the datetime objects and merging both the list using.

list1.addAll(list2);

I want to now sort this arraylist on the order of the date time object that the arraylist consists of. Can anyone guide me how to sort this arraylist.

Rahul Kalidindi
  • 4,666
  • 14
  • 56
  • 92

3 Answers3

5
ArrayList<Date> myDates = new ArrayList<>();
// populate the List... 
Collections.sort(myDates);

If you are using Joda Time Date time, this is done the same way, since those classes usually implement Comparable.

Eugene
  • 117,005
  • 15
  • 201
  • 306
5

If you meant they have Java Date object. You can just do Collections#sort()

Nishant
  • 54,584
  • 13
  • 112
  • 127
0

You can define your comparator for the objects of the arraylist, like if I am using objects having structure

and then you can use, sort method directly.

jeet
  • 29,001
  • 6
  • 52
  • 53