当前位置:网站首页>Comparator(用于Arrays.sort)
Comparator(用于Arrays.sort)
2022-06-25 10:57:00 【用户9854323】
package snippet;
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorUse {
public static void main(String[] args) {
Student[] persons = new Student[5];
persons[0] = new Student("tom", 1, 88, 45);
persons[1] = new Student("jack", 6, 80, 12);
persons[2] = new Student("bill", 4, 68, 21);
persons[3] = new Student("kandy", 2, 98, 34);
persons[4] = new Student("lily", 5, 94, 20);
System.out.println("排序前的数据:");
for (Student student : persons) {
System.out.println(student);
}
//这里是关键
Arrays.sort(persons, new SortByNumber());
System.out.println("根据学生编号由低到高排序:");
for (Student student : persons) {
System.out.println(student);
}
//这里是关键
Arrays.sort(persons, new SortByScore());
System.out.println("根据学生成绩由高到低排序:");
for (Student student : persons) {
System.out.println(student);
}
}
//这里是关键
static class SortByNumber implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getNumber() - o2.getNumber();
}
}
//这里是关键
static class SortByScore implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}
}
输出:
排序前的数据:
Student[name:tom,age:45,number:1,score:88]
Student[name:jack,age:12,number:6,score:80]
Student[name:bill,age:21,number:4,score:68]
Student[name:kandy,age:34,number:2,score:98]
Student[name:lily,age:20,number:5,score:94]
根据学生编号由低到高排序:
Student[name:tom,age:45,number:1,score:88]
Student[name:kandy,age:34,number:2,score:98]
Student[name:bill,age:21,number:4,score:68]
Student[name:lily,age:20,number:5,score:94]
Student[name:jack,age:12,number:6,score:80]
根据学生成绩由高到低排序:
Student[name:kandy,age:34,number:2,score:98]
Student[name:lily,age:20,number:5,score:94]
Student[name:tom,age:45,number:1,score:88]
Student[name:jack,age:12,number:6,score:80]
Student[name:bill,age:21,number:4,score:68]
student类:
package snippet;
class Student {
private String name;
private int number;
private int score;
private int age;
public Student(String name,int number,int score,int age){
this.name = name;
this.number = number;
this.score = score;
this.age = age;
}
@Override
public String toString() {
return "Student[name:"+name+",age:"+age+",number:"+number+",score:"+score+"]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
边栏推荐
- Learn to learn self-study [learning to learn itself is more important than learning anything]
- FPGA基于VGA显示字符及图片
- OODA working method
- ARM64汇编的函数有那些需要注意?
- Apache ShenYu 入门
- [file containing vulnerability-03] six ways to exploit file containing vulnerabilities
- Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
- Binder explanation of Android interview notes
- 手机炒股安全吗?
- Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
猜你喜欢
Binder explanation of Android interview notes
ES 学习
TASK03|概率论
Query method and interrupt method to realize USART communication
Writing wechat applet with uni app
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
SystemVerilog(十三)-枚举数据类型
Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
A difficult mathematical problem baffles two mathematicians
随机推荐
A random number generator
Performance network
Daily 3 questions (2) - find out the lucky numbers in the array
1-7snapshots and clones in VMWare
keep-alive
手机办理广州证券开户靠谱安全吗?
GaussDB 如何统计用户sql的响应时间
Apache ShenYu 入門
How to start the phpstudy server
Binder explanation of Android interview notes
[file containing vulnerability-03] six ways to exploit file containing vulnerabilities
每日3题(2)- 找出数组中的幸运数
戴尔科技演绎“快”字诀,玩转CI/CD
《天天数学》连载52:二月二十日
数组结构整理
持续交付-Jenkinsfile 语法
金仓KFS数据级联场景部署
Is it safe to open a securities account in changtou school by mobile phone?
Technical practice and development trend of video conference all in one machine
Detection and analysis of simulator in an app