java 根据日期自定义列表排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6198916/
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-30 14:47:06  来源:igfitidea点击:

Custom sorting of list based on date

javaarraylistcompare

提问by zax

I got:

我有:

List<Comment> list = new ArrayList<Comment>();

My class Comment contains: id, name and date; I got some function that adds specified Comments, but I would like to order after that my list by date descending. How to do it with this:

我的班级评论包含:id、姓名和日期;我有一些添加指定评论的函数,但我想在我的列表之后按日期降序排序。如何做到这一点:

Collections.sort(list, comparator)

?

?

采纳答案by aioobe

You'll have to implement a Comparator<Comment>that implements the comparemethod based on the date field, and pass this object to the Collections.sort.

您必须实现基于日期字段Comparator<Comment>compare方法,并将此对象传递给Collections.sort.

Here's a complete example:

这是一个完整的例子:

import java.util.*;

class Comment {
    String id, String name;
    Date date;

    public Comment(String id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }
}


class CommentComparator implements Comparator<Comment> {
    public int compare(Comment o1, Comment o2) {
        return o1.date.compareTo(o2.date);
    }
}


class Test {
    public static void main(String[] args) {
        List<Comment> comments = new ArrayList<Comment>() {{
            long now = System.currentTimeMillis();
            add(new Comment("id1", "second", new Date(now)));
            add(new Comment("id2", "first", new Date(now - 1000)));
            add(new Comment("id3", "third", new Date(now + 1000)));
        }};

        Collections.sort(comments, new CommentComparator());
    }
}

回答by bruno conde

You already know what you need to do. Just implement de Comparator. It could be something like:

你已经知道你需要做什么。只需执行 de Comparator。它可能是这样的:

Collections.sort(list, new Comparator<Comment>() {
            @Override
            public int compare(Comment o1, Comment o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });

(You may need to check for nulls)

(您可能需要检查空值)

回答by Tomasz B?achowicz

One option mentioned by fellows here is to implement custom Comparator for the comments. Alternative approach would be to implement Comparableinterface in Comment class.

这里的研究员提到的一种选择是为评论实现自定义比较器。另一种方法是在 Comment 类中实现Comparable接口。

import java.util.*;

class Comment implements Comparable<Comment> {
    private String id, String name;
    private Date date;

    public Comment(String id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }

    public int compareTo(Comment comment) {
        return date.compareTo(o2.date); //Look Ma! Date is Comparable too!
    }

}


class Test {
    public static void main(String[] args) {
        List<Comment> comments = new ArrayList<Comment>() {{
            long now = System.currentTimeMillis();
            add(new Comment("id1", "second", new Date(now)));
            add(new Comment("id2", "first", new Date(now - 1000)));
            add(new Comment("id3", "third", new Date(now + 1000)));
        }};
        Collections.sort(comments);
    }
}

I have used some of the @aioobe code in my example.

我在示例中使用了一些 @aioobe 代码。