list集合排序

对于list集合排序 

第一种

//默认排序
Collections.sort(list);

第二种

实现Comparable 接口  重写 compareTo 方法即可

@Override
  public int compareTo(User user) {          
    return this.age - user.getAge();  
  }

第三种 

自定义比较器 

 Comparator<NewWeeklyInformation> comparator = new Comparator<NewWeeklyInformation>() {
                public int compare(NewWeeklyInformation s1, NewWeeklyInformation s2) {
                    // 先排日期
                    if (s1.getStartDate().substring(0, 10) != s2.getStartDate().substring(0, 10)) {
                        return s1.getStartDate().substring(0, 10).compareTo(s2.getStartDate().substring(0, 10));
                    }  else {
                        // 日期相同 按创建事件
                        return s1.getCreateTime().compareTo(s2.getCreateTime());
                    }
                }
            };
Collections.sort(newWeeklyInformations,comparator);
原文地址:https://www.cnblogs.com/bdjsdl/p/13139249.html