本帖最后由 adamjy 于 2014-4-22 12:36 编辑
代码如下,
- public class Employee implements Comparator {
- private String Name;
- private int Salary;
- public String getName() {
- return Name;
- }
- public Employee() {
- super();
- // TODO Auto-generated constructor stub
- }
- public void setName(String name) {
- Name = name;
- }
- public int getSalary() {
- return Salary;
- }
- public void setSalary(int salary) {
- Salary = salary;
- }
- public static Comparator<Employee> Comparesal = new Comparator<Employee>() {
- @Override
- public int compare(Employee o1, Employee o2) {
- // TODO Auto-generated method stub
- return (int) (o1.getSalary() - o2.getSalary());
- }
- };
- public static Comparator<Employee> Comparename = new Comparator<Employee>() {
- @Override
- public int compare(Employee o1, Employee o2) {
- // TODO Auto-generated method stub
- return (o1.getName().compareTo(o2.getName()));
- }
- };
- @Override
- public int compare(Object o1, Object o2) {
- // TODO Auto-generated method stub
- return 0;
- }
- public Employee(String name, int salary) {
- super();
- this.Name = name;
- this.Salary = salary;
- }
- public static void main(String args[]) {
- Employee[] emparr = new Employee[3];
- emparr[0] = new Employee("Adam", 30000);
- emparr[1] = new Employee("John", 40000);
- emparr[2] = new Employee("Gates", 50000);
- Arrays.sort(emparr, Employee.Comparename);
- System.out.println(Arrays.toString(emparr));
- }
- }
复制代码
打印结果,“[com.itheima.Employee@c0dd841, com.itheima.Employee@60f09995, com.itheima.Employee@53f336eb]”
为什么不是“Adam,Gates,John” |