当前位置:网站首页>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;
}
}边栏推荐
- ARM64特有一些的汇编指令
- 10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
- Advanced single chip microcomputer -- development of PCB (2)
- 【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
- Apache ShenYu 入門
- [the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
- 软件测试 避免“试用期被辞退“指南,看这一篇就够了
- 看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
- 金仓数据库 KingbaseES 插件force_view
- 每日3題(3)-檢查整數及其兩倍數是否存在
猜你喜欢

报名开启|飞桨黑客马拉松第三期如约而至,久等啦

数据库系列:MySQL索引优化总结(综合版)

软件测试 避免“试用期被辞退“指南,看这一篇就够了

Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.

zabbix分布式系统监控

中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析

Android之Kotlin语法详解与使用

Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis

中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析

今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
随机推荐
【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
ZABBIX distributed system monitoring
服务端渲染
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-7
Kingbasees plug-in ftutilx of Jincang database
CSRF攻击
软件测试 避免“试用期被辞退“指南,看这一篇就够了
Cdn+cos ultra detailed steps for drawing bed construction
Daily 3 questions (3) - check whether integers and their multiples exist
Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
Output reading: apply what you have learned
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
Socket communication principle
Dell technology performs the "fast" formula and plays ci/cd
贝叶斯
Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)