当前位置:网站首页>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;
}
}边栏推荐
- FPGA displays characters and pictures based on VGA
- Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
- 戴尔科技演绎“快”字诀,玩转CI/CD
- 每日3题(3)-检查整数及其两倍数是否存在
- [file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?
- At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
- XSS attack
- Jincang database kingbasees plug-in force_ view
- June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
- zabbix分布式系统监控
猜你喜欢

金仓KFS数据级联场景部署

A difficult mathematical problem baffles two mathematicians

网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
![[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?](/img/28/ab02d38bde47053b155e0545b47039.png)
[file inclusion vulnerability-04] classic interview question: how to getshell when a website is known to have only local file inclusion vulnerability?

仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)

Oracle彻底卸载的完整步骤

SystemVerilog (XIII) - enumerate data types

Open source invites you to participate in the openssf Open Source Security Online Seminar

Explanation and use of kotlin syntax for Android

【文件包含漏洞-03】文件包含漏洞的六种利用方式
随机推荐
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
Comparator(用于Arrays.sort)
Dell technology performs the "fast" formula and plays ci/cd
MySQL and Oracle processing CLOB and blob fields
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
ARM64汇编的函数有那些需要注意?
Is it safe for Guosen Securities to open a securities account
Daily 3 questions (3) - check whether integers and their multiples exist
每日3題(3)-檢查整數及其兩倍數是否存在
XSS攻击
视频会议一体机的技术实践和发展趋势
金仓数据库 KingbaseES 插件dbms_session
Learn to learn self-study [learning to learn itself is more important than learning anything]
Socket communication principle
FPGA基于VGA显示字符及图片
Android之Kotlin语法详解与使用
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
Free access to the global human settlements layer (ghsl) dataset from Gee
Es learning
MySQL synchronous data configuration and shell script implementation