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.
3 Answers 3
If you meant they have Java Date object. You can just do Collections#sort()
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
.
-
1No need for a custom comparator.JB Nizet– JB Nizet2012年02月02日 07:53:44 +00:00Commented Feb 2, 2012 at 7:53
-
Almost the same? What's the difference?Charles Wood– Charles Wood2013年07月17日 19:16:55 +00:00Commented Jul 17, 2013 at 19:16
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.
Collections.sort(list1);
should do the trick for you.