Joda DateTime 数组按日期时间对数组排序

Nic*_*uir 1 sorting datetime arraylist jodatime comparator

我有一个 Joda DateTimes 的数组列表,如下所示:

List <DateTime> nextRemindersArray = new ArrayList<DateTime>();
nextRemindersArray.add(reminderOneDateTime);
nextRemindersArray.add(reminderTwoDateTime);
nextRemindersArray.add(reminderThreeDateTime);
Run Code Online (Sandbox Code Playgroud)

我正在尝试按升序对日期进行排序,但遇到了麻烦:

我用谷歌搜索并找到了这个页面:

https://cmsoftwaretech.wordpress.com/2015/07/19/sort-date-with-timezone-format-using-joda-time/

我尝试过这样的:

nextRemindersArray.sort(nextRemindersArray);
Run Code Online (Sandbox Code Playgroud)

但它给了我错误:

Error:(1496, 37) error: incompatible types: List<DateTime> cannot be converted to Comparator<? super DateTime>
Run Code Online (Sandbox Code Playgroud)

然后我尝试这样:

DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();
nextRemindersArray.sort(nextRemindersArray, dateTimeComparator);
Run Code Online (Sandbox Code Playgroud)

也像这样:

nextRemindersArray.sort(nextRemindersArray, new DateTimeComparator());
Run Code Online (Sandbox Code Playgroud)

但都有错误。

我尝试了 Joda 时间手册,但没有太大帮助。如何对数组进行排序?

在此先感谢您的帮助

ass*_*ias 5

您正在寻找的是:

nextRemindersArray.sort(DateTimeComparator.getInstance());
Run Code Online (Sandbox Code Playgroud)

但因为DateTime已经实现了Comparable,你实际上不需要比较器,你可以简单地使用:

nextRemindersArray.sort(null); //uses natural sorting
//or probably more readable
Collections.sort(nextRemindersArray);
Run Code Online (Sandbox Code Playgroud)

请注意,快速浏览一下的文档List::sort就会发现该方法只需要一个参数,并且它必须是一个比较器(而不是像您的问题中那样有两个参数)。