根据list集合某个字段进行排序
Posted 宇翊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据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; } 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) -> Double.compare(x.getAge(), y.getAge())); for (Student stu : students) { System.out.println(stu); } } }
以上是关于根据list集合某个字段进行排序的主要内容,如果未能解决你的问题,请参考以下文章