我有問題找到一種有效的方式按順序排序類。我的下面的代碼完成了我需要排序的順序,但我相信有另一種方法(我不知道)。如何比較訂單類?
什麼是排序類的有效方法?
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;
}
的排列順序是 SalaryEmployee - > WeeklyEmployee - > DailyEmployee作爲第一重點 grossPay作爲第二個關鍵 – Jcpz23