简体   繁体   English

根据日期时间对对象的数组列表进行排序

[英]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. 我有两个由datetime对象组成的arraylists和合并使用两个列表。

list1.addAll(list2);

I want to now sort this arraylist on the order of the date time object that the arraylist consists of. 我现在想按该arraylist组成的日期时间对象的顺序对该arraylist进行排序。 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 . 如果您使用的是Joda Time Date时间,则以相同的方式进行操作,因为这些类通常实现Comparable

If you meant they have Java Date object. 如果您的意思是他们有Java Date对象。 You can just do Collections#sort() 你可以做Collections#sort()

You can define your comparator for the objects of the arraylist, like if I am using objects having structure 您可以为arraylist的对象定义比较器,就像我使用具有结构的对象一样

and then you can use, sort method directly. 然后您可以直接使用sort方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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