-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTestSort.java
More file actions
36 lines (33 loc) · 969 Bytes
/
myTestSort.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接口
import java.math.BigDecimal;
import java.util.Arrays;
public class sortMyTest {
public static void main(String[] args) {
Person[] ps = new Person[] {
new Person("Bob", 61.5),
new Person("Alice", 61.2),
new Person("Lily", 75),
};
Arrays.sort(ps);
System.out.println(Arrays.toString(ps));
}
}
class Person implements Comparable<Person> {
String name;
double score;
Person(String name, double score) {
this.name = name;
this.score = score;
}
@Override
public int compareTo(Person other) {
//这里每次new两个对象,如果数据量大,肯定是慢的。
BigDecimal a = new BigDecimal(this.score);
BigDecimal b = new BigDecimal(other.score);
return a.compareTo(b);
}
@Override
public String toString() {
return this.name + "," + this.score;
}
}