2016-11-20 113 views
0

我乔达DateTime是否像这样一个ArrayList:乔达日期时间数组排序数组按日期时间

List <DateTime> nextRemindersArray = new ArrayList<DateTime>(); 
nextRemindersArray.add(reminderOneDateTime); 
nextRemindersArray.add(reminderTwoDateTime); 
nextRemindersArray.add(reminderThreeDateTime); 

我试图以升序日期排序,但我有麻烦:

我用Google搜索并找到了这个网页:

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

我想它是这样的:

nextRemindersArray.sort(nextRemindersArray); 

但它给我的错误:

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

我又试图像这样:

DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance(); 
nextRemindersArray.sort(nextRemindersArray, dateTimeComparator); 

而且这样的:

nextRemindersArray.sort(nextRemindersArray, new DateTimeComparator()); 

但都有错误。

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

在此先感谢您的帮助

回答

2

你所寻找的是:

nextRemindersArray.sort(DateTimeComparator.getInstance()); 

但由于DateTime已经实现Comparable,你并不真的需要一个比较,你可以简单地使用:

nextRemindersArray.sort(null); //uses natural sorting 
//or probably more readable 
Collections.sort(nextRemindersArray); 

请注意,快速查看the documentation of List::sort会告诉您,该方法只需要一个参数它必须是一个比较器(而不是你的问题中的两个参数)。

+0

非常感谢你非常感谢! –