按日期排序特殊字符串的数组列表 [英] sort array list of special strings, by date
问题描述
我有一个数组列表.
这是一个字符串数组列表.该字符串包含一个 "Date.toString"
,格式为 "January 1, 1970, 00:00:00 GMT"
+ 任务名称.
This is an arrayList of strings. The string contains a "Date.toString"
in format of "January 1, 1970, 00:00:00 GMT"
+ task name.
例如:
"January 1, 1970, 00:00:00 GMT clean the house".
我想按日期对这个 arrayList 进行排序.
我该怎么做?
I want to sort this arrayList by dates.
How can I do it?
推荐答案
您可以编写一个比较器来解析日期并使用 date.compareTo(otherDate)
,但我建议你首先存储 dates
而不是 Strings
,这样排序更容易(Date 实现 Comparable
.
You could write a comparator that parses the dates and sorts them using date.compareTo(otherDate)
, but I'd suggest you store dates
instead of Strings
in the first place, making sorting much easier (Date implements Comparable<Date>
.
(如果您的输入格式是String
,则在将它们添加到列表时转换Strings
)
(If your input format is String
, then convert the Strings
at the time you add them to the list)
这篇关于按日期排序特殊字符串的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!