排序特殊字符串的数组列表 [英] sort array list of special strings

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

问题描述


我有一个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

推荐答案

您必须将字符串的日期部分转换为真实的DateTime结构.对字符串进行排序不会对您有所帮助.实现一个包含您的字符串以及转换后的DateTime的类,并使其实现Comparable接口.这样,您可以将它们放入名为say myList的ArrayList实例中,并通过调用Collections.sort(myList)为您进行排序.

干杯!

-MRB
You''ll have to convert the date part of the string to a real DateTime struct. Sorting strings isn''t going to help you here. Implement a class that holds your string as well as the converted DateTime and make it implement the Comparable interface. This way you can put them in to a ArrayList instance named say myList and they''ll be sorted for you by calling Collections.sort(myList).

Cheers!

-MRB


此列表包含字符串-仅此而已.

您需要提取所需的子字符串 [ ^ ]并通过对它们进行评分来对列表进行排序.这可以通过if/else或switch命令的模糊逻辑来完成.

冒泡排序@ leepoint.net
[^ ]

选择排序@ leepoint.net [
This list contains Strings - nothing more.

you need to extract the wanted substring(s!) [^]and sort the list by rating them. This can be done with a fuzzy logic of if/else or switch commands.

Bubble sort @ leepoint.net
[^]

Selection Sort @ leepoint.net[^]

Have fun - and when you''ve written some code and get stuck - come back and ask specific. We do help - but we also want you to do the homework yourself.

regards
Torsten


将字符串加载到Comparable对象中:
Load your string into a Comparable object:
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆