777

package Collections.sort;

import java.util.Comparator;

public class Employee implements Comparable<Employee>{
	int id;
	String name;
	int age;
	
	public Employee(int id, String name,int age) {
		super();
		this.id = id;
		this.name = name;
		this.age=age;
	}

	@Override
	public int compareTo(Employee o) {
		int result=this.id-o.id;
		int result2=this.age-o.age;
		if(result!=0){
			return result;
		}else if(result2!=0){
			return result2;
		}else{
			return this.name.compareTo(o.name);
		}
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	//内部类     根据id的比较结果进行排序
	static class IdComparator implements Comparator<Employee>{
		@Override
		public int compare(Employee o1, Employee o2) {
//			return o1.id.compareTo(o2.id);
			return o1.id-o2.id;
		}		
	}
	
	
	
	
}
 

  

posted @ 2017-04-17 19:21  奋斗的少年WH  阅读(186)  评论(0编辑  收藏  举报