Java将list集合按某个字段排序import java.util.ArrayList;
import java.util.List;
class Student {
private String name;
private double age;
public Student(String name, double age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;java集合排序怎么实现
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
@Override
public String toString() {
return "Perosn [name=" + name + ", age=" + age + "]";
}
}
public class ListSort {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("abc", 12.124));
students.add(new Student("bcd", 20.214));
students.add(new Student("cde", 17.321));
students.add(new Student("def", 25.145));
students.add(new Student("efg", 15.145));
students.sort((x, y) -> Age(), y.getAge()));//这⽅法需要jdk1.8以上
for (Student stu : students) {
System.out.println(stu);
}
}
}
students.sort((x, y) -> Age(), y.getAge()));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论