2.Java 比较器

 

1.比较器

定义一个比较器,需要实现 Comparator 接口的 compare() 方法,compare() 方法的返回值类型为 int

1
2
3
4
5
Comparator<T> nameComparator = new Comparator<T>() {
public int compare(T o1, T o2) {
return 0;
}
};

(1) 升序排序:

1
2
3
4
5
6
7
8
9
if(o1 > o2) {
return >0;
}
if(o1 == o2) {
return 0;
}
if(o1 < o2) {
return <0;
}

(2) 降序排序:

1
2
3
4
5
6
7
8
9
if(o1 > o2) {
return <0;
}
if(o1 == o2) {
return 0;
}
if(o1 < o2) {
return >0;
}

2.用比较器实现对集合的排序

(1) 定义 Student 类

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
public class Student {

private int id;

private String name;

private int age;

private int score;

public Student() {

}

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

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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 int getScore() {
return score;
}

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

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

(2) 对 ArrayList<Student> 集合进行排序

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
69
70
71
72
73
74
75
76
Student student1 = new Student(1, "d1", 11, 200);
Student student2 = new Student(2, "a2", 12, 300);
Student student3 = new Student(3, "f3", 13, 15);
Student student4 = new Student(4, "f4", 14, 10);
Student student5 = new Student(5, "b5", 15, 50);
Student student6 = new Student(6, "c6", 16, 100);

ArrayList<Student> studentList = new ArrayList<Student>();

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
studentList.add(student6);

// define a name comparator
Comparator<Student> nameComparator = new Comparator<Student>() {
public int compare(Student s1, Student s2) {
String name1 = s1.getName();
String name2 = s2.getName();
int result = name1.compareTo(name2);
return result;
}
};

// 1.按姓名排序(升序)
Collections.sort(studentList, nameComparator);

for (Student student: studentList) {
System.out.println(student);
}
// Student{id=2, name='a2', age=12, score=300}
// Student{id=5, name='b5', age=15, score=50}
// Student{id=6, name='c6', age=16, score=100}
// Student{id=1, name='d1', age=11, score=200}
// Student{id=3, name='f3', age=13, score=15}
// Student{id=4, name='f4', age=14, score=10}

// 2.按分数排序(升序)
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int score1 = s1.getScore();
int score2 = s2.getScore();
return score1 - score2;
}
});

for (Student student: studentList) {
System.out.println(student);
}
// Student{id=4, name='f4', age=14, score=10}
// Student{id=3, name='f3', age=13, score=15}
// Student{id=5, name='b5', age=15, score=50}
// Student{id=6, name='c6', age=16, score=100}
// Student{id=1, name='d1', age=11, score=200}
// Student{id=2, name='a2', age=12, score=300}

// 3.按年龄排序(降序)
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int age1 = s1.getAge();
int age2 = s2.getAge();
return age2 - age1;
}
});

for (Student student: studentList) {
System.out.println(student);
}
// Student{id=6, name='c6', age=16, score=100}
// Student{id=5, name='b5', age=15, score=50}
// Student{id=4, name='f4', age=14, score=10}
// Student{id=3, name='f3', age=13, score=15}
// Student{id=2, name='a2', age=12, score=300}
// Student{id=1, name='d1', age=11, score=200}