The Java comparator comparator USES sample sharing
- 2020-04-01 03:05:47
- OfStack
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorTest implements Comparator<stuEntity> {
public static void main(String[] args) {
List<stuEntity> list = new ArrayList<stuEntity>();
stuEntity stud1=new stuEntity();
stud1.setAge(10);
stud1.setName("abc");
stuEntity stud2=new stuEntity();
stud2.setAge(10);
stud2.setName("bdc");
stuEntity stud3=new stuEntity();
stud3.setAge(5);
stud3.setName("bdd");
stuEntity stud4=new stuEntity();
stud4.setAge(30);
stud4.setName("aad");
list.add(stud1);
list.add(stud2);
list.add(stud3);
list.add(stud4);
Collections.sort(list, new ComparatorTest());
for(stuEntity stud:list){
System.out.println(stud.getAge()+":"+stud.getName());
}
}
@Override
public int compare(stuEntity stud1, stuEntity stud2) {
//Sort by name
int maxname=stud1.getName().compareTo(stud2.getName());
if(maxname!=0)
return maxname;
//Sort by age
int maxage=stud1.getAge()-stud2.getAge();
//if(maxage!=0)
return maxage;
}
}
Output:
30:aad
10:abc
10:bdc
5:bdd
The Java Comparator is useful, implementing the compare () callback method on the Comparator interface to make the collation, and then calling collections.sort (list, new ComparatorTest()); I can sort the List, very handy
Note the order in which the returns in the compare () method are used, with the precedence collation first
Entity class:
public class stuEntity {
private int studentId;//Student id
private String name;
private int age;
private String sex;//gender
private int roomNumber;//Your room number,
private String degree;//A degree in
private int grade;//grade
private String deviceNumber;//Device number
private int groupNumber;//group
private int javaScore;//Java performance
private int netScore;//The NET result
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public stuEntity(String name, int age, String sex, int roomNumber,
String deviceNumber, int groupNumber, int javaScore, int netScore) {
this.name = name;
this.age = age;
this.sex = sex;
this.roomNumber = roomNumber;
this.deviceNumber = deviceNumber;
this.groupNumber = groupNumber;
this.javaScore = javaScore;
this.netScore = netScore;
}
public stuEntity() {
}
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
public int getNetScore() {
return netScore;
}
public void setNetScore(int netScore) {
this.netScore = netScore;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public String getDeviceNumber() {
return deviceNumber;
}
public void setDeviceNumber(String deviceNumber) {
this.deviceNumber = deviceNumber;
}
public int getGroupNumber() {
return groupNumber;
}
public void setGroupNumber(int groupNumber) {
this.groupNumber = groupNumber;
}
}