排序日期 - 用户输入

发布于 2024-10-10 03:32:30 字数 146 浏览 0 评论 0原文

我正在使用 Java 创建一个交互式应用程序来询问用户的出生日期。目前,我正在读取一个日期并将其存储为表单中的字符串(dd-mm-yyyy)。现在有多个人存储在一个数组列表中,每个人都有不同的出生日期。根据出生日期对数组列表进行排序的最佳方法是什么?

亲切的问候

I'm using Java to create an interactive application to ask users for their date of birth. At the minute I have a date being read in and stored as a String in the form (dd-mm-yyyy). Now there are mutliple people being stored in an arraylist, all with different dates of birth. Whats the best way to sort the arraylist depending on their dates of birth?

Kind Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人疚 2024-10-17 03:32:31

您是否在列表或日期中存储 Person 对象?如果存储日期(java.util.Date),您可以简单地对列表进行排序,它应该适合您。如果您存储 Person 对象,则可以传入排序实用程序将使用的自定义 Comparator

参考文献:

Are you storing Person objects in the list or dates? If storing dates (java.util.Date), you can simply sort the list and it should work out fine for you. If you store Person objects, you can pass in a custom Comparator which would be used by the sorting utility.

References:

仅一夜美梦 2024-10-17 03:32:31

首先将字符串转换为 java.util.Date。日期的排序语义与字符串不同。

完成此操作后,编写一个比较器来为您完成这项工作。将其与您的列表一起传递给 Collections.sort()。

Start by converting the String to a java.util.Date. Dates have different semantics for sorting than Strings do.

Once you do that, write a Comparator to do the job for you. Pass it along with your List to Collections.sort().

墟烟 2024-10-17 03:32:31

将日期存储在 Date 对象中。它们开箱即可分类。或者,如果保留字符串就足够了,请将日期格式从 更改

dd-mm-yyyy

为 来

yyyy-mm-dd

进行排序。

Store the dates in Date objects. They are sortable out of the box. Or, if it's sufficient to keep Strings, change the date format from

dd-mm-yyyy

to

yyyy-mm-dd

for sorting.

烈酒灼喉 2024-10-17 03:32:31

Collections.sort(集合、比较器);

实现您的自定义比较器(接口java.util.Comparator),根据出生日期比较您的对象。

Collections.sort(collection, comparator);

implement your custom comparator (interface java.util.Comparator) that compares your object according to date of birth.

神回复 2024-10-17 03:32:31

stack over flow中已经提供了一个链接:http://stackoverflow.com/questions/1517745/sorting-on-last-name

There is a link already provided in stack over flow:http://stackoverflow.com/questions/1517745/sorting-on-last-name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文