- public class Students {
- private int age;
- private int score;
- public Students(int age, int score){
- super();
- this.age = age;
- this.score = score;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getScore() {
- return score;
- }
- public void setScore(int score) {
- this.score = score;
- }
- }
- public static void main(String[] args) {
- List<Students> students = new ArrayList<Students>();
- students.add(new Students(23, 100));
- students.add(new Students(27, 98));
- students.add(new Students(29, 99));
- students.add(new Students(29, 98));
- students.add(new Students(22, 89));
- Collections.sort(students, new Comparator<Students>() {
- // getScore() 排序字段
- @Override
- public int compare(Students o1, Students o2) {
- int i = o1.getScore() - o2.getScore(); // 按照分数排序
- if(i == 0){
- return o1.getAge() - o2.getAge(); // 按照年龄排序
- }
- return i;
- }
- });
- for(Students stu : students){
- System.out.println(”score:” + stu.getScore() + “:age” + stu.getAge());
- }
- }
Java list 对象 排序
最新推荐文章于 2024-04-11 23:50:10 发布