2013-06-24 31 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方法對於沒有預定義比較方式的對象很有用。