40 lines
884 B
Java
40 lines
884 B
Java
package com.inmind.collections04;
|
|
|
|
public class Student implements Comparable<Student>{
|
|
String name;
|
|
int age;
|
|
int score;
|
|
|
|
public Student(String name, int age, int score) {
|
|
this.name = name;
|
|
this.age = age;
|
|
this.score = score;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "Student{" +
|
|
"name='" + name + '\'' +
|
|
", score=" + score +
|
|
'}';
|
|
}
|
|
|
|
/*
|
|
排序口诀:
|
|
我(this)-它(参数o):升序
|
|
它-我:降序
|
|
*/
|
|
@Override
|
|
public int compareTo(Student o) {
|
|
// return this.score - o.score; //升序
|
|
/* if (this.score > o.score) {
|
|
return -1;
|
|
} else if (this.score < o.score) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}*/
|
|
return o.score - this.score;//降序
|
|
}
|
|
}
|