根据对象中值的自定义优先级对对象列表进行排序 JAVA 11
Sort a list of Object according to custom priority of value in the Object JAVA 11
我有一个 Student(name, result, rollNo) 个对象的列表。
List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133"),
);
我需要按照以下要求对上面的列表进行排序(我使用的是 Java 11)
- 按照学生的成绩排序,顺序为“未通过”、“通过”、“不可用”(未按字母顺序排列)
成绩不及格的学生应该排在榜首。然后“通过”,然后“不可用”
- 如果多个同学结果相同,则按rollNo升序排序。
最终结果应该如下图所示。
{"Joe", "Fail", "111"}
{"Matt", "Fail", "115"}
{"Sam", "Pass", "100"}
{"Ann", "Pass", "121"}
{"Moss", "Pass", "133"}
{"Gill", "Not available", "101"}
从Java 8开始我们可以对多个字段进行排序流
如果您想按优先级“未通过”、“通过”、“不可用”进行排序,一种替代方法是在学生中添加一个额外的字段。这将有助于在没有自然顺序时决定排序的优先级。在这个例子中,学生按“通过”、“失败”、“不可用”排序,然后按姓名排序,只是为了显示更多选项。
排序代码片段:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
上下文中的代码片段:
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
输出:
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
在学生中添加字段:
private int priority;
在class学生中添加了排序优先级的方法:
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
上下文中的方法 getPriority():
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
我有一个 Student(name, result, rollNo) 个对象的列表。
List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133"),
);
我需要按照以下要求对上面的列表进行排序(我使用的是 Java 11)
- 按照学生的成绩排序,顺序为“未通过”、“通过”、“不可用”(未按字母顺序排列) 成绩不及格的学生应该排在榜首。然后“通过”,然后“不可用”
- 如果多个同学结果相同,则按rollNo升序排序。
最终结果应该如下图所示。
{"Joe", "Fail", "111"}
{"Matt", "Fail", "115"}
{"Sam", "Pass", "100"}
{"Ann", "Pass", "121"}
{"Moss", "Pass", "133"}
{"Gill", "Not available", "101"}
从Java 8开始我们可以对多个字段进行排序流
如果您想按优先级“未通过”、“通过”、“不可用”进行排序,一种替代方法是在学生中添加一个额外的字段。这将有助于在没有自然顺序时决定排序的优先级。在这个例子中,学生按“通过”、“失败”、“不可用”排序,然后按姓名排序,只是为了显示更多选项。
排序代码片段:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
上下文中的代码片段:
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
输出:
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
在学生中添加字段:
private int priority;
在class学生中添加了排序优先级的方法:
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
上下文中的方法 getPriority():
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}