import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
/**
* @author Lei
* @version create time:2009-10-14 上午11:42:07
*/
public class ArrayListQueue {
public static void display(Collection<Student> c) {
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
Student o = it.next();
System.out.println(o.toString());
}
}
public static void main(String[] args) {
Student stu1 = new Student(1, "zhangsan", "male", 23, "cs");
Student stu2 = new Student(2, "lisi", "female", 19, "cs");
Student stu3 = new Student(3, "wangwu", "male", 20, "cs");
Student stu4 = new Student(4, "zhaoliu", "female", 21, "cs");
Student stu5 = new Student(5, "xiaoming", "male", 22, "cs");
ArrayList<Student> list = new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
Comparator<Student> comparator = new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
if(s1.getId() != s2.getId()) {
return s1.getId() - s2.getId();
}
else if(!s1.getName().equals(s2.getName())) {
return s1.getName().compareTo(s2.getName());
}
else if(s1.getAge() != s2.getAge()) {
return s1.getAge() - s2.getAge();
}
return 0;
}
};
Collections.sort(list, comparator);
display(list);
}
}
class Student {
private int id;
private String name;
private String sex;
private int age;
private String team;
public Student(int id, String name, String sex, int age, String team) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.team = team;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public String getTeam() {
return team;
}
public String toString() {
return id + name + sex + age + team;
}
}