java Collections工具类 sort 根据对象中的值 排序

一、sort 方法重写

1、语法

// 1.输入 new Comparator,自动重写
// 2.o1 -o2 升序, o2 - o1 降序
Collections.sort(list, new Comparator<Demon06Person>() {
    @Override
    public int compare(Demon06Person o1, Demon06Person o2) {
        // 正序
        return o1.getAge() - o2.getAge();
    }
});

2、案例

a、对象类

package com.wt.collection;

public class Demon06Person {
    private String name;
    private Integer age;

    public Demon06Person() {

    }

    public Demon06Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Demon06Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

b、主类

package com.wt.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demon06 {
    public static void main(String[] args) {
        Demon06Person p1 = new Demon06Person("风剑", 8);
        Demon06Person p2 = new Demon06Person("小葵", 2);
        Demon06Person p3 = new Demon06Person("正南", 6);
        Demon06Person p4 = new Demon06Person("园长", 49);
        ArrayList<Demon06Person> list = new ArrayList<>();
        Collections.addAll(list, p1, p2, p3, p4);
        System.out.println("list = " + list);

        Collections.sort(list, new Comparator<Demon06Person>() {
            @Override
            public int compare(Demon06Person o1, Demon06Person o2) {
                // 正序
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println("list = " + list);
    }
}

二、对象实现 Comparator

1、核心

定义的对象实现Comparable,重写 compareTo方法, 强制类型转换

@Override
public int compareTo(Object o) {
    if(o instanceof Student){
        Student student = (Student) o;
        return this.getScore() - student.getScore();
    }else {
        return -1;
    }
}

2、案例

a、对象类

package com.wt.collection;

import java.util.Comparator;

public class Student implements Comparable {
    private String name;
    private Integer score;

    public Student() {

    }

    public Student(String name, Integer score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }


    @Override
    public int compareTo(Object o) {
        if(o instanceof Student){
            Student student = (Student) o;
            return this.getScore() - student.getScore();
        }else {
            return -1;
        }
    }
}

b、主类

package com.wt.collection;

import java.util.ArrayList;
import java.util.Collections;

public class Demon07 {
    public static void main(String[] args) {
        Student student1 = new Student("红", 90);
        Student student2 = new Student("白", 50);
        Student student3 = new Student("紫", 70);
        Student student4 = new Student("绿", 30);
        ArrayList<Student> students = new ArrayList<>();
        Collections.addAll(students, student1, student2, student3, student4);
        System.out.println("students = " + students);
        Collections.sort(students);
        System.out.println("students = " + students);
    }
}

 

posted @ 2025-04-18 22:27  市丸银  阅读(42)  评论(0)    收藏  举报