当前位置:网站首页>Comparator (for arrays.sort)
Comparator (for arrays.sort)
2022-06-25 11:14:00 【User 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(" Data before sorting :");
for (Student student : persons) {
System.out.println(student);
}
// Here's the key
Arrays.sort(persons, new SortByNumber());
System.out.println(" Sort by student number from low to high :");
for (Student student : persons) {
System.out.println(student);
}
// Here's the key
Arrays.sort(persons, new SortByScore());
System.out.println(" According to the students' grades from high to low :");
for (Student student : persons) {
System.out.println(student);
}
}
// Here's the key
static class SortByNumber implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getNumber() - o2.getNumber();
}
}
// Here's the key
static class SortByScore implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}
}
Output :
Data before sorting :
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]
Sort by student number from low to high :
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]
According to the students' grades from high to low :
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 class :
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;
}
}
边栏推荐
- Upload and modify the use of avatars
- 基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
- ZABBIX distributed system monitoring
- Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
- A random number generator
- Jincang KFS data cascade scenario deployment
- Is it safe for Guosen Securities to open a securities account
- 数组结构整理
- Google Earth Engine (Gee) - evaluate réalise le téléchargement en un clic de toutes les images individuelles dans la zone d'étude (certaines parties de Shanghai)
- Nuxtjs actual combat case
猜你喜欢
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
ZABBIX distributed system monitoring
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
Previous string inversion topic
网络远程访问的方式使用树莓派
Nuxtjs actual combat case
CSRF攻击
软件测试 避免“试用期被辞退“指南,看这一篇就够了
Crawler scheduling framework of scratch+scratch+grammar
1-7snapshots and clones in VMWare
随机推荐
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-7
How gaussdb counts the response time of user SQL
Query method and interrupt method to realize USART communication
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
某APP中模拟器检测分析
金仓数据库 KingbaseES 插件DBMS_OUTPUT
scrapy+scrapyd+gerapy 爬虫调度框架
Netease's open source distributed storage system curve officially became the CNCF sandbox project
Gaussdb cluster maintenance case set - slow SQL execution
Kingbasees plug-in DBMS of Jincang database_ session
16 enterprise architecture strategies
【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
A five-year technical Er, based on the real experience of these years, gives some suggestions to the fresh students
Advanced single chip microcomputer -- development of PCB (2)
Comparator(用于Arrays.sort)
1-7snapshots and clones in VMWare
Writing wechat applet with uni app
数组结构整理
金仓数据库 KingbaseES 插件DBMS_RANDOM
戴尔科技演绎“快”字诀,玩转CI/CD