【问题标题】:Custom sorting of list based on date根据日期自定义列表排序
【发布时间】:2011-06-01 09:22:19
【问题描述】:

我明白了:

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

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

Collections.sort(list, comparator)

?

【问题讨论】:

    标签: java arraylist compare


    【解决方案1】:

    您必须实现一个Comparator&lt;Comment&gt;,该Comparator&lt;Comment&gt; 基于日期字段实现compare 方法,并将此对象传递给Collections.sort。

    这是一个完整的例子:

    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());
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      (您可能需要检查空值)

      【讨论】:

        【解决方案3】:

        这里研究员提到的一个选项是为 cmets 实现自定义 Comparator。另一种方法是在 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);
            }
        }
        

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

        【讨论】:

          猜你喜欢
          • 2014-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多