public class Student {
private String name;
private int age;
private int score;
public Student() {
super();
}
public Student(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score
+ "]";
}
}
public class Test_SelectSort {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int score = s2.getScore() - s1.getScore();
int age = score == 0? s1.getAge() - s2.getAge() : score;
return age == 0? s1.getName().compareTo(s2.getName()) : age;
}
});
ts.add(new Student("Tom", 24, 89));
ts.add(new Student("Robin", 32, 99));
ts.add(new Student("Jerry", 24, 99));
ts.add(new Student("Lili", 23, 87));
ts.add(new Student("Jack", 22, 87));
ts.add(new Student("Lilei", 25, 95));
ts.add(new Student("Robin", 32, 99));
for (Student student : ts) {
System.out.println(student);
}
}
} |