【发布时间】:2020-06-30 21:47:38
【问题描述】:
import java.util.Date;
public class Emp {
public Emp() {
}
private int Id;
private String name;
private Date date_of_join;
private String city;
private int age;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate_of_join() {
return date_of_join;
}
public void setDate_of_join(Date date_of_join) {
this.date_of_join = date_of_join;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我有一个有 5 个字段的 bean。我想按照 1 比 2 比 3 比 4 比 5 对所有五个字段进行排序
它由
字符串 细绳, 日期, 细绳 , 诠释。
如何根据 ID、姓名、加入日期、城市、年龄对列表中的 list<Emp> 进行排序
【问题讨论】:
-
Comparator.comparing(YourObject::getFirstString).thenComparing(YourObject::getSecondString).... -
@Eugene 是否有任何集合 api 的内置方法,因为我正在使用 Comparator
compareById = (Emp o1, Emp o2) -> o1.getId().compareTo( o2.getId ()); Collections.sort(Emp, compareById); -
@Eugene 以上代码仅对 id 进行排序。
-
Collections.sort(employees, Comparator.comparing.....)
标签: java sorting java-8 comparator comparable