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.

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()

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

No need for a custom comparator.
Almost the same? What's the difference?
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.

Comments

Your Answer

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.