s-day02-Collections里面的sort方法以及Comparable接口
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user