复制代码代码如下:
importar java.util.arraylist;
importar java.util.Collections;
importar java.util.comparator;
importar java.util.list;
public class ComparatORTEST IPLEMEIRA COMPARADOR <STUENTITY> {
/**
* @param args
*/
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);
Coleções.Sort (List, New ComparatTest ());
para (Stuentity Stud: List) {
System.out.println (stud.getage ()+":"+stud.getName ());
}
}
/**
*
*/
@Override
Public Int Compare (Stuentity Stud1, Stuentity Stud2) {
// 根据姓名排序
int maxname = stud1.getName (). compareto (stud2.getName ());
if (maxname! = 0)
retornar maxname;
// 根据年龄排序
int maxage = stud1.getage ()-stud2.getage ();
// if (maxage! = 0)
retornar maxage;
}
}
输出
复制代码代码如下:
30: Aad
10: ABC
10: BDC
5: BDD
Java 的比较器很有用 , 实现 Comparador 接口的 Compare () 这个回调方法来制定排序规则 , 然后调用 Coleções
使用时要注意 Compare () 方法中的 Retorno 的先后顺序 , 优先的排序规则要写在前面
实体类
复制代码代码如下:
/**
* 学生实体类
*
*/
classe pública Stuentity {
privado int studentID; // 学号
nome de string privado;
private Int Age;
sexo privado de corda; // 性别
private int RoomNumber; // 房间号
grau de cordas privadas; // 学位
Private Int Grade; // 年级
private string deviceNumber; // 设备号
private int groupNumber; // 所属的小组
privado int javascore; // java 成绩
private Int NetsCore; // net 成绩
public String getDegree () {
grau de retorno;
}
public void setDegree (grau de string) {
this.Degree = grau;
}
public int getGrade () {
nota de retorno;
}
public void SetGrade (Int Grade) {
this.Grade = GRADE;
}
/**
* 初始化有参构造函数
*
* @param id
* Nome @param
* @param Age
* @param sexo
* @param RoomNumber
* @param deviceNumber
* @param groupNumber
* @param javascore
* @param netscore
*/
Public Stuentity (nome da string, idade int, sexo de cordas, intmonumber,
String deviceNumber, int groupNumber, int javascore, int netscore) {
this.name = nome;
this.age = idade;
this.sex = sexo;
this.roomNumber = RoomNumber;
this.DeviceNumber = deviceNumber;
this.GroupNumber = GroupNumber;
this.javascore = javascore;
this.NetsCore = NetScore;
}
/**
* 无参构造函数
*/
public Stuentity () {
}
public int getJavascore () {
devolver Javascore;
}
public void setJavascore (int javascore) {
this.javascore = javascore;
}
public int getNetScore () {
retorno NetScore;
}
public void setNetsCore (int netscore) {
this.NetsCore = NetScore;
}
public int getStudentId () {
Retornar StudentID;
}
public void setStudentId (int studentId) {
this.studentId = StudentId;
}
public String getName () {
Nome de retorno;
}
public void setName (nome da string) {
this.name = nome;
}
public int getage () {
idade de retorno;
}
Public Void Setage (Int Age) {
this.age = idade;
}
public String getSex () {
retornar sexo;
}
public void setSex (sexo de string) {
this.sex = sexo;
}
public int getroomNumber () {
retornar o número do quarto;
}
public void SetroomNumber (int RoomNumber) {
this.roomNumber = RoomNumber;
}
public String getDeviceNumber () {
retornar deviceNumber;
}
public void setDeviceNumber (String deviceNumber) {
this.DeviceNumber = deviceNumber;
}
public int getGroupNumber () {
retornar o groupNumber;
}
public void setGroupNumber (int groupNumber) {
this.GroupNumber = GroupNumber;
}
}