本帖最后由 新手ing 于 2015-7-11 21:39 编辑
import java.util.*;
class Stu {
private String stuCla;
private String stuId;
public String getStuCla() {
return stuCla;
}
public void setStuCla(String stuCla) {
this.stuCla = stuCla;
}
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
Stu(String stuCla,String stuId){
this.stuCla = stuCla;
this.stuId = stuId;
}
}
public class Demo {
public static void main(String[] args) {
Stu stu1 = new Stu("100001","20");
Stu stu2 = new Stu("100002","21");
Stu stu3 = new Stu("100001","22");
Stu stu4 = new Stu("100003","21");
Stu stu5 = new Stu("100003","19");
Stu stu6 = new Stu("100004","21");
Stu stu7 = new Stu("100004","24");
List<Stu> list = new ArrayList<Stu>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
list.add(stu6);
list.add(stu7);
Comparator cmp = new Comparator(){
public int compare(Object o1, Object o2) {
Stu stu1 = (Stu)o1;
Stu stu2 = (Stu)o2;
int i = stu1.getStuCla().compareTo(stu2.getStuCla());
if(i==0){
i = stu1.getStuId().compareTo(stu2.getStuId());
}
return i;
}
};
Collections.sort(list, cmp);
Iterator it = list.iterator();
while(it.hasNext()){
Stu stu = (Stu)it.next();
System.out.println(stu.getStuCla()+" "+stu.getStuId());
}
}
}
|
|