import java.util.*;
class student{
int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
public student(){
}
public student(int number,String name){
this.name=name;
this.number=number;
}
}
class studentCom<T> implements Comparator<T>{
String type="";
public studentCom(){
}
public studentCom(String type){
this.type=type;
}
@Override
public int compare(Object o1, Object o2) {
student s1=(student)o1;
student s2=(student)o2;
if(type.equals("xuehao")){
return s1.number-s2.number;//从小到大
}else if(type.equals("xingming")){
return s1.name.compareTo(s2.name);
}else if(type.equals("special")){
return s2.name.compareTo(s1.name);
}else{
return 0;
}
}
}
public class Test1 {
public static void main(String[] args) {
String type="";//按照什么排序/学号、姓名、成绩等
student s1=new student(92,"ggg");
student s2=new student(99,"ggg");
student s3=new student(90,"ggg");
student s4=new student(100,"bbb");
student s5=new student(32,"zzzz");
student s6=new student(20,"hhhh");
ArrayList<student> list =new ArrayList<student>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
System.out.println("--------按照姓名排序-------");
type="xingming";
Collections.sort(list,new studentCom<student>(type));
for(student ss:list){
System.out.println(ss.name+" "+ss.number);
}
System.out.println("--------按照学号排序-------");
type="xuehao";
Collections.sort(list,new studentCom<student>(type));
for(student ss:list){
System.out.println(ss.name+" "+ss.number);
}
}
}