简体   繁体   中英

Sorting arraylist of objects based on the date time

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.

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM