2013-06-24 32 views
0

我有问题找到一种有效的方式按顺序排序类。我的下面的代码完成了我需要排序的顺序,但我相信有另一种方法(我不知道)。如何比较订单类?

什么是排序类的有效方法?

public int compare(Object one, Object two)  
{ 
      //S = Salaried, W = Weekly, D = Daily 

      //SS == 0 -> SW == -1 -> SD == -1 
      //WS == 1 -> WW == 0 -> WD == -1 
      //DS == 1 -> DW == 1 -> DD == 0 

      Employee a = (Employee)one; 
      Employee b = (Employee)two; 

      SalariedEmployee s = new SalariedEmployee(0.0); 
      WeeklyEmployee w = new WeeklyEmployee (0.0); 
      DailyEmployee d = new DailyEmployee(); 


      if(one.getClass() == s.getClass() && two.getClass() == s.getClass()) 
       return Double.compare(b.grossPay(), a.grossPay()); 

      if(one.getClass() == s.getClass() && two.getClass() == w.getClass()) 
       return -1; 

      if(one.getClass() == s.getClass() && two.getClass() == d.getClass()) 
       return -1; 

      if(one.getClass() == w.getClass() && two.getClass() == s.getClass()) 
       return 1; 

      if(one.getClass() == w.getClass() && two.getClass() == w.getClass()) 
       return Double.compare(b.grossPay(), a.grossPay()); 

      if(one.getClass() == w.getClass() && two.getClass() == d.getClass()) 
       return -1; 

      if(one.getClass() == d.getClass() && two.getClass() == s.getClass()) 
       return 1; 

      if(one.getClass() == d.getClass() && two.getClass() == w.getClass()) 
       return 1; 

      if(one.getClass() == d.getClass() && two.getClass() == d.getClass()) 
       return Double.compare(b.grossPay(), a.grossPay()); 

      return 0; 

     } 
+0

的排列顺序是 SalaryEmployee - > WeeklyEmployee - > DailyEmployee作为第一重点 grossPay作为第二个关键 – Jcpz23

回答

0

我有问题,找到一个有效的方式由顺序排序类

取决于你的意思是什么“高效”。从CPU角度来看,将所有代码放在单个方法中将是最有效的(如果正确完成),但从灵活性的角度来看,效率并不高。

对于不会很快但会更加灵活的方法,请查看Group ComparatorBean Comparator

GroupComparator允许您将多个比较器组合为一种类型。 BeanComparator是一个通用的比较器,允许您在给定类中的任何字段进行排序。因此,要使用GroupComparator的基本代码是:

EmployeeComparator employee = new EmployeeComparator(); 
BeanComparator grossPay = new BeanComparator(Employee.class, "grossPay"); 
GroupComparator gc = new GroupComparator(employee, grossPay); 
Collections.sort(list, gc); 

所以,你会需要编写的工薪,每周和每日排序员工一个比较。对于EmployeeComparator的基本代码可能是这样的:

if (one.getClass()equals(two.getClass()) 
    return 0; 

if (one instanceOf SalariedEmployee) 
    return 1; 

if (two instanceOf SalariedEmployee) 
    return -1; 

if (one instanceOf WeeklyEmployee) 
    return 1; 
else 
    return -1; 

多做点工作来设置,但一旦你有一个EmployeeComparator然后你可以使用bean和组比较排序的多个不同的属性。

3

实现可比<>接口在类和重写Employee类compareTo()方法。该方法将Object类作为传递值。例如,

public class Employee implements Comparable<Employee> { 
    //omitted 

    public int compareTo(Employee other) { 
     return grossPay.compareTo(other.grossPay); 
    } 
} 

请查看以下链接了解更多 http://download.oracle.com/javase/tutorial/collections/interfaces/order.html

+0

我很欣赏你的反应,然而,这只是解决了我的grossPay问题,而不是Order类。所以基本上有两个订单薪水 - >每周 - >每日课,然后总薪酬。 例如工资500每周550每日450。 – Jcpz23

0

这是我的解决方案。

public int compare(Employee left, Employee right) { 
    int typeOrderLeft = getTypeOrder(left); 
    int typeOrderRight = getTypeOrder(right); 

    if (typeOrderLeft == typeOrderRight) { 
     return Double.compare(left.grossPay(), right.grossPay()); 
    } else { 
     return typeOrderLeft - typeOrderRight; 
    } 
} 

private int getTypeOrder(Employee employee) { 
    if (employee instanceof DailyEmployee) { 
     return 1; 
    } else if (employee instanceof WeeklyEmployee) { 
     return 2; 
    } else if (employee instanceof SalaryEmployee) { 
     return 3; 
    } 

    return 0; 
} 
0

您需要首先实现可比较的接口。这可以让你定义一个compareTo方法,它可以用来根据你认为可比较类的特定值对类进行排序。

定义compareTo方法对于没有预定义比较方式的对象很有用。