java 按降序或升序排列日期的 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14155417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:16:23  来源:igfitidea点击:

Order ArrayList of dates in decending or ascending order

javadatearraylist

提问by Munazza

I have an array list of Dates and I want to order it by dates in ascending and descending order. Could anyone please provide a complete code snippet for this ? I have seen many questions like this but could not find a suitable answer.

我有一个日期数组列表,我想按日期按升序和降序对其进行排序。任何人都可以为此提供完整的代码片段吗?我见过很多这样的问题,但找不到合适的答案。

List will be like this

列表将是这样的

         List<Date> dates= new ArrayList<Date>();

Any kind of help will be appreciated , Many thanks,

任何形式的帮助将不胜感激,非常感谢,

回答by assylias

Since Datealready implements Comparable, you can simply use:

由于Date已经实现了 Comparable,您可以简单地使用:

Collections.sort(dates);

to sort in ascending order, and for descending order:

按升序和降序排序:

Collections.sort(dates, Collections.reverseOrder());