- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTestSort.java
More file actions
Latest commit
36 lines (33 loc) · 969 Bytes
/
Copy pathmyTestSort.java
File metadata and controls
36 lines (33 loc) · 969 Bytes
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
## 程序功能: 实现sort接口
importjava.math.BigDecimal;
importjava.util.Arrays;
publicclasssortMyTest {
publicstaticvoidmain(String[] args) {
Person[] ps = newPerson[] {
newPerson("Bob", 61.5),
newPerson("Alice", 61.2),
newPerson("Lily", 75),
};
Arrays.sort(ps);
System.out.println(Arrays.toString(ps));
}
}
classPersonimplementsComparable<Person> {
Stringname;
doublescore;
Person(Stringname, doublescore) {
this.name = name;
this.score = score;
}
@Override
publicintcompareTo(Personother) {
//这里每次new两个对象,如果数据量大,肯定是慢的。
BigDecimala = newBigDecimal(this.score);
BigDecimalb = newBigDecimal(other.score);
returna.compareTo(b);
}
@Override
publicStringtoString() {
returnthis.name + "," + this.score;
}
}