其他分享
首页 > 其他分享> > list集合根据某字段进行排序

list集合根据某字段进行排序

作者:互联网

//升序排列
Collections.sort(list, new Comparator<Student>() {
  public int compare(Student s1, Student s2) {
    return s1.getAge().compareTo(s2.getAge());
  }
});
//降序排列
Collections.sort(list, new Comparator<Student>() {
  public int compare(Student s1, Student s2) {
    return s2.getAge().compareTo(s1.getAge());
  }
});

//使用下面需要JDK1.8及以上
//升序排列
list.sort((x,y)->Integer.compare(x.getAge(), y.getAge()));
//降序排列
list.sort((x,y)->Integer.compare(y.getAge(), x.getAge()));

标签:compare,某字段,s2,s1,list,getAge,Student,排序
来源: https://www.cnblogs.com/sunline/p/15641499.html