class expert implements Comparable<expert> {
private int age;
public expert(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(expert e) {
return this.age - e.getAge();
}
}
public class test03 {
public static void main(String [] args){
expert e1 = new expert(1);
expert e2 = new expert(2);
expert e3 = new expert(3);
ArrayList<expert> list = new ArrayList<expert>();
list.add(e3);
list.add(e2);
list.add(e1);
Collections.sort(list);
for(expert li:list){
System.out.print(li.getAge());
}
}
}
collections.sort方法使用的是归并排序java6,timesortjava7