日期:2014-05-20 浏览次数:20952 次
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("abc", 5000);
staff[1] = new Employee("efg", 2000);
staff[2] = new Employee("opq", 3500);
Arrays.sort(staff);
for(Employee e: staff)
{
System.out.println("name = " + e.getName() + ", and his salary is "+ e.getSalary());
}
}
}
class Employee implements Comparable<Employee>
{
public Employee(String n, double s)
{
name = n;
salary = s;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int compareTo(Employee other)
{
if(salary < other.salary)
{
return -1;
}
if(salary > other.salary)
{
return 1;
}
return 0;
}
private String name ;
private double salary;
}
public int compareTo(Employee other)
{
if(salary < other.salary)
{
return -1;
}
if(salary > other.salary)
{
return 1;
}
return 0;
}
public int compareTo(Employee other)
{
if(salary < other.salary)
{
return -1;
}
if(salary > other.salary)
{
return 1;
}
return 0;
}
public int compareTo(Employee other)
{
if(this.salary < other.salary)
{
return -1;
}
if(this.salary > other.salary)
{
return 1;
}
return 0;
}