Java源码-Comparable

当需要对一个对象进行排序时,该对象应该实现Comparable接口,并实现其唯一的方法:int compareTo(T o)
在该方法中定义自己的排序规则,当调用Arrays.sort(Object[] a)方法时则回调compareTo()方法,并按照自己的规则对对象数组进行排序。

返回值及比较规则:
1、返回负值---->小于
2、返回零------>等于
3、返回正值---->大于

例子:
public class Worker implements Comparable<Worker>{
	private String name;  
    private int age;  
    private int salary;  
      
    public Worker(String name, int age, int salary) {  
        super();  
        this.name = name;  
        this.age = age;  
        this.salary = salary;  
    }     
  
    public String getName() {  
        return name;  
    }  
      
    public int getAge() {  
        return age;  
    }  
  
    public int getSalary() {  
        return salary;  
    }  
  
    /** 
     *实现Comparable接口的compareTo方法,在此方法中定义自己的比较规则 
     * 首先按工资有由低到高排序,过工资相同则按年龄由高到底排序 
     */  
    @Override  
    public int compareTo(Worker other) {  
        // TODO Auto-generated method stub  
        if(this.salary < other.getSalary()){     //工资小于其他人时返回负值  
            return -1;  
        }else if(this.salary > other.getSalary()){   //工资大于其他人时返回正值  
            return 1;  
        }else{                                      //工资等于其他人时按照年龄再排序  
            if(this.age < other.getAge()){  
                return 1;//表示由高到低排序  
            }else if(this.age >= other.getAge()){  
                return -1;  
            }  
        }  
        return 0;         
    }  
      
    @Override  
    public String toString() {  
        // TODO Auto-generated method stub  
        return this.name +"\t" + this.age + "\t" + this.salary;  
    }
}


测试
public class Demo {
	public static void main(String[] args) {
		 Worker [] workers = new Worker[5];  
	        workers[0] = new Worker("test1",25,2000);  
	        workers[1] = new Worker("test2",24,2100);  
	        workers[2] = new Worker("test3",26,2100);  
	        workers[3] = new Worker("test4",27,2200);  
	        workers[4] = new Worker("test5",28,1900);  
	          
	        //调用Arrays.sort()方法进行排序  
	        [color=red]Arrays.sort(workers);[/color]  
	        for(Worker w : workers){  
	            System.out.println(w);  
	        }
	}
}


测试结果
test5	28	1900
test1	25	2000
test3	26	2100
test2	24	2100
test4	27	2200

你可能感兴趣的:(java源码)