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.

asked Feb 2, 2012 at 7:43
3
  • @naveen -- it's tagged with java Commented Feb 2, 2012 at 7:47
  • Collections.sort(list1); should do the trick for you. Commented Feb 2, 2012 at 7:49
  • Possible dupe of stackoverflow.com/questions/5927109/… Commented Feb 2, 2012 at 7:51

3 Answers 3

5

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

answered Feb 2, 2012 at 7:51
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.

answered Feb 2, 2012 at 7:51
2
  • 1
    No need for a custom comparator. Commented Feb 2, 2012 at 7:53
  • Almost the same? What's the difference? Commented Jul 17, 2013 at 19:16
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.

answered Feb 2, 2012 at 7:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.