本文介绍了排序特殊字符串的数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个arrayList.
这是字符串的arrayList.该字符串包含格式为"1970年1月1日,格林尼治标准时间00:00:00"和任务名称的"Date.toString".
例如:格林尼治标准时间1970年1月1日,00:00:00打扫房屋".
我想按日期对arrayList进行排序.
我该怎么办?

Hi,
I have an arrayList.
This is an arrayList of strings. The string contains a "Date.toString" in format of "January 1, 1970, 00:00:00 GMT" + task name.
For example: "January 1, 1970, 00:00:00 GMT clean the house".
I want to sort this arrayList by dates.
How can I do it?

Thanks

推荐答案



class Task implements Comparable<Task> {
    private Date date;
    private String task;
    Task (String taskString) {
        // find the split point
        int splitPoint = 45;
        try {
            this.date =
                    DateFormat.getInstance().
                    parse(taskString.substring(0, splitPoint));
        } catch (ParseException ex) {
            this.date = new Date();
        }
        this.task = taskString.substring(splitPoint);
    }
    public int compareTo(Task o) {
        int compare = this.date.compareTo(o.date);
        if (compare == 0) {
            compare = this.task.compareTo(o.task);
        }
        return compare;
    }
}



然后将其放入ArrayList.这些是使用Collections.sort排序的.



Then put this into a ArrayList. These are ordered using Collections.sort.


这篇关于排序特殊字符串的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:01