33import java .util .ArrayList ;
44import java .util .Arrays ;
55import java .util .Comparator ;
6- import java .util .TreeMap ;
76
87public class ShowComparator {
98
@@ -19,148 +18,87 @@ public Student(String name, int id, int age) {
1918 }
2019 }
2120
22- // 任何比较器:
23- // compare方法里,遵循一个统一的规范:
24- // 返回负数的时候,认为第一个参数应该排在前面
25- // 返回正数的时候,认为第二个参数应该排在前面
26- // 返回0的时候,认为无所谓谁放前面
27- public static class IdShengAgeJiangOrder implements Comparator <Student > {
21+ // 谁id大,谁放前!
22+ public static class IdComparator implements Comparator <Student > {
2823
29- // 根据id从小到大,但是如果id一样,按照年龄从大到小
24+ // 如果返回负数,认为第一个参数应该排在前面
25+ // 如果返回正数,认为第二个参数应该排在前面
26+ // 如果返回0,认为谁放前面无所谓
3027 @ Override
3128 public int compare (Student o1 , Student o2 ) {
32- return o1 .id != o2 .id ? (o1 .id - o2 .id ) : (o2 .age - o1 .age );
29+ if (o1 .id < o2 .id ) {
30+ return 1 ;
31+ } else if (o2 .id < o1 .id ) {
32+ return -1 ;
33+ } else {
34+ return 0 ;
35+ }
3336 }
34-
35- }
36-
37- public static class IdAscendingComparator implements Comparator <Student > {
38-
39- // 返回负数的时候,第一个参数排在前面
40- // 返回正数的时候,第二个参数排在前面
41- // 返回0的时候,谁在前面无所谓
42- @ Override
43- public int compare (Student o1 , Student o2 ) {
44- return o1 .id - o2 .id ;
45- }
46-
47- }
48-
49- public static class IdDescendingComparator implements Comparator <Student > {
50-
51- @ Override
52- public int compare (Student o1 , Student o2 ) {
53- return o2 .id - o1 .id ;
54- }
55-
5637 }
38+
39+ // 谁age大,谁放前!
40+ public static class AgeComparator implements Comparator <Student > {
5741
58- // 先按照id排序,id小的,放前面;
59- // id一样,age大的,前面;
60- public static class IdInAgeDe implements Comparator <Student > {
61-
42+ // 如果返回负数,认为第一个参数应该排在前面
43+ // 如果返回正数,认为第二个参数应该排在前面
44+ // 如果返回0,认为谁放前面无所谓
6245 @ Override
6346 public int compare (Student o1 , Student o2 ) {
64- return o1 .id != o2 .id ? o1 .id - o2 .id : (o2 .age - o1 .age );
65- }
66-
67- }
68-
69- public static void printStudents (Student [] students ) {
70- for (Student student : students ) {
71- System .out .println ("Name : " + student .name + ", Id : " + student .id + ", Age : " + student .age );
47+ if (o1 .age < o2 .age ) {
48+ return 1 ;
49+ } else if (o2 .age < o1 .age ) {
50+ return -1 ;
51+ } else {
52+ return 0 ;
53+ }
7254 }
7355 }
7456
75- public static void printArray (Integer [] arr ) {
76- if (arr == null ) {
77- return ;
78- }
57+ public static void printArray (int [] arr ) {
7958 for (int i = 0 ; i < arr .length ; i ++) {
8059 System .out .print (arr [i ] + " " );
8160 }
8261 System .out .println ();
8362 }
8463
85- public static class MyComp implements Comparator <Integer > {
86-
87- @ Override
88- public int compare (Integer o1 , Integer o2 ) {
89- return o2 - o1 ;
90- }
91-
92- }
93-
94- public static class AComp implements Comparator <Integer > {
95-
96- // 如果返回负数,认为第一个参数应该拍在前面
97- // 如果返回正数,认为第二个参数应该拍在前面
98- // 如果返回0,认为谁放前面都行
99- @ Override
100- public int compare (Integer arg0 , Integer arg1 ) {
101-
102- return arg1 - arg0 ;
103-
104- // return 0;
64+ public static void printStudents (Student [] students ) {
65+ for (int i = 0 ; i < students .length ; i ++) {
66+ System .out .println (students [i ].name + ", " + students [i ].id + ", " + students [i ].age );
10567 }
106-
10768 }
10869
10970 public static void main (String [] args ) {
110-
111- Integer [] arr = { 5 , 4 , 3 , 2 , 7 , 9 , 1 , 0 };
112-
113- Arrays .sort (arr , new AComp ());
114-
115- for (int i = 0 ; i < arr .length ; i ++) {
116- System .out .println (arr [i ]);
117- }
118-
119- System .out .println ("===========================" );
120-
121- Student student1 = new Student ("A" , 4 , 40 );
122- Student student2 = new Student ("B" , 4 , 21 );
123- Student student3 = new Student ("C" , 3 , 12 );
124- Student student4 = new Student ("D" , 3 , 62 );
125- Student student5 = new Student ("E" , 3 , 42 );
126- // D E C A B
127-
128- Student [] students = new Student [] { student1 , student2 , student3 , student4 , student5 };
129- System .out .println ("第一条打印" );
130-
131- Arrays .sort (students , new IdShengAgeJiangOrder ());
132- for (int i = 0 ; i < students .length ; i ++) {
133- Student s = students [i ];
134- System .out .println (s .name + "," + s .id + "," + s .age );
135- }
136-
137- System .out .println ("第二条打印" );
138- ArrayList <Student > studentList = new ArrayList <>();
139- studentList .add (student1 );
140- studentList .add (student2 );
141- studentList .add (student3 );
142- studentList .add (student4 );
143- studentList .add (student5 );
144- studentList .sort (new IdShengAgeJiangOrder ());
145- for (int i = 0 ; i < studentList .size (); i ++) {
146- Student s = studentList .get (i );
147- System .out .println (s .name + "," + s .id + "," + s .age );
71+ int [] arr = { 8 , 1 , 4 , 1 , 6 , 8 , 4 , 1 , 5 , 8 , 2 , 3 , 0 };
72+ printArray (arr );
73+ Arrays .sort (arr );
74+ printArray (arr );
75+
76+ Student s1 = new Student ("张三" , 5 , 27 );
77+ Student s2 = new Student ("李四" , 1 , 17 );
78+ Student s3 = new Student ("王五" , 4 , 29 );
79+ Student s4 = new Student ("赵六" , 3 , 9 );
80+ Student s5 = new Student ("左七" , 2 , 34 );
81+
82+ Student [] students = { s1 , s2 , s3 , s4 , s5 };
83+ printStudents (students );
84+ System .out .println ("=======" );
85+ Arrays .sort (students , new IdComparator ());
86+ printStudents (students );
87+ System .out .println ("=======" );
88+
89+ ArrayList <Student > arrList = new ArrayList <>();
90+ arrList .add (s1 );
91+ arrList .add (s2 );
92+ arrList .add (s3 );
93+ arrList .add (s4 );
94+ arrList .add (s5 );
95+ for (Student s : arrList ) {
96+ System .out .println (s .name + ", " + s .id + ", " + s .age );
14897 }
149- // N * logN
150- System .out .println ("第三条打印" );
151- student1 = new Student ("A" , 4 , 40 );
152- student2 = new Student ("B" , 4 , 21 );
153- student3 = new Student ("C" , 4 , 12 );
154- student4 = new Student ("D" , 4 , 62 );
155- student5 = new Student ("E" , 4 , 42 );
156- TreeMap <Student , String > treeMap = new TreeMap <>((a , b ) -> (a .id - b .id ));
157- treeMap .put (student1 , "我是学生1,我的名字叫A" );
158- treeMap .put (student2 , "我是学生2,我的名字叫B" );
159- treeMap .put (student3 , "我是学生3,我的名字叫C" );
160- treeMap .put (student4 , "我是学生4,我的名字叫D" );
161- treeMap .put (student5 , "我是学生5,我的名字叫E" );
162- for (Student s : treeMap .keySet ()) {
163- System .out .println (s .name + "," + s .id + "," + s .age );
98+ System .out .println ("=======" );
99+ arrList .sort (new AgeComparator ());
100+ for (Student s : arrList ) {
101+ System .out .println (s .name + ", " + s .id + ", " + s .age );
164102 }
165103
166104 }
0 commit comments