java 重写compareTo方法实现类排序

class expert implements Comparable {

   
    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 list = new ArrayList();
		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

你可能感兴趣的:(java基础)