复制代码代码如下:
java.util.arraylistをインポートします。
java.util.collectionsをインポートします。
java.util.comparatorをインポートします。
java.util.listをインポートします。
public class comparetortestを実装するコンパレーター<Stuentity> {
/**
* @param args
*/
public static void main(string [] args){
リスト<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 comparetortest());
for(stuentity stud:list){
system.out.println(stud.getage()+":"+stud.getName());
}
}
/**
*
*/
@オーバーライド
public int compare(stuentity stud1、stuentity stud2){
//根据姓名排序
int maxname = stud1.getname()。比較(stud2.getname());
if(maxname!= 0)
return maxname;
//根据年龄排序
int maxage = stud1.getage() - stud2.getage();
// if(maxage!= 0)
マックスを返します。
}
}
输出:
复制代码代码如下:
30:aad
10:ABC
10:BDC
5:BDD
java的比较器很有用、实现比較接口的比較()这个回调方法来制定排序规则、然后调用コレクション。sort(list、new comparatortest());
使用时要注意比較()方法中的 return的先后顺序、优先的排序规则要写在前面
实体类:
复制代码代码如下:
/**
*学生实体类
*
*/
パブリッククラスのスタンティティ{
private int dustentid; //学号
プライベート文字列名;
プライベートインクエイジ;
プライベートストリングセックス; //性别
Private int roomnumber; //房间号
プライベート文字列の学位; //学位
プライベートINTグレード; //年级
Private String Devicenumber; //设备号
Private int GroupNumber; //所属的小组
Private int javascore; // java成绩
Private int netscore; //net成绩
public string getDegree(){
戻り度;
}
public void setdegree(string degree){
this.degree = degree;
}
public int getGrade(){
返品グレード。
}
public void setGrade(int grade){
this.grade = grade;
}
/**
*初始化有参构造函数
*
* @param id
* @param名
* @param Age
* @paramセックス
* @param roomnumber
* @param devicenumber
* @param GroupNumber
* @Param JavaScore
* @Param Netscore
*/
パブリックスタンティティ(文字列名、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(){
JavaScoreを返します。
}
public void setjavascore(int javascore){
this.javascore = javascore;
}
public int getnetscore(){
NetScoreを返します。
}
public void setnetscore(int netscore){
this.netscore = netscore;
}
public int getstudentid(){
retuntuntsid;
}
public void setStudentid(int dustentid){
this.studentid = dustentId;
}
public string getName(){
返品名;
}
public void setName(string name){
this.name = name;
}
public int getage(){
戻り年齢;
}
public void Setage(int age){
this.age = age;
}
public string getSex(){
セックスを返す;
}
public void setsex(string sex){
this.sex = sex;
}
public int getroomNumber(){
ルームナンバーを返します。
}
public void setroomnumber(int roomnumber){
this.roomnumber = roomnumber;
}
public string getDevicEnumber(){
DevicEnumberを返します。
}
public void setDevicEnumber(String DevicEnumber){
this.devicenumber = devicenumber;
}
public int getGroupNumber(){
GroupNumberを返します。
}
public void setGroupNumber(int GroupNumber){
this.groupNumber = groupNumber;
}
}