Home compareTo()在排序中的应用
Post
Cancel

compareTo()在排序中的应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double score;

    public Student(String name, int age, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    public String toString() {
        return "name=" + name + ", age=" + age + ", score=" + score;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getScore() {
        return score;
    }

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

    // 重写compareTo
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    public static void main(String[] args) {
        Student s1 = new Student("Bob", 13, 50);
        Student s2 = new Student("Tom", 15, 40);
        Student s3 = new Student("Jhon", 20, 80);
        Student s4 = new Student("Ann", 10, 60);

        Student[] stu = new Student[4];
        stu[0] = s1;
        stu[1] = s2;
        stu[2] = s3;
        stu[3] = s4;

        //利用Arrays.sort对数组进行排序的时候按照重写的compareTo方法进行
        Arrays.sort(stu);

        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i]);
        }
     }
}

运行结果:

1
2
3
4
name=Ann, age=10, score=60.0
name=Bob, age=13, score=50.0
name=Tom, age=15, score=40.0
name=Jhon, age=20, score=80.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    // 重写compareTo方法
    public int compareTo(Student o) {
        // 先按age排序
        if (this.age > o.getAge()) {
            return (this.age - o.getAge());
        }
        if (this.age < o.getAge()) {
            return (o.getAge() - this.age);
        }
        // 再按name排序
        if (this.name.compareTo(o.getName()) > 0) {
            return 1;
        }
        if (this.name.compareTo(o.getName()) < 0) {
            return -1;
        }
        return 0;
    }

    public static void main(String[] args) {
        Student f1 = new Student("c", 15);
        Student f2 = new Student("a", 25);
        Student f3 = new Student("b", 15);

        List<Student> list = new ArrayList<Student>();
        list.add(f1);
        list.add(f3);
        list.add(f2);

        Collections.sort(list);

        for (Student o : list) {
            System.out.println(o.getAge() + "-->" + o.getName());
        }
    }
}

运行结果:

1
2
3
15-->b
15-->c
25-->a
This post is licensed under CC BY 4.0 by the author.