我有一個jobArray數組,其中包含一個description
,hours needed to complete
,hourly pay that a user enters
的5個作業。c#使用IComparable接口對數組排序
我需要按總費用(hours * hourly pay)
的升序排列數組。
這個任務需要我使用IComparable interface
,但我不確定如何使用它。任何幫助,將不勝感激,謝謝
這裏是我job
類
class Job : IComparable
{
public Job(string description, int hours, double hourRate, double fee)
{
Description = description;
hoursToComplete = hoursToComplete;
hourlyRate = hourlyRate;
totalFee = totalFee;
}
這是我做過比較totalFees
接口public int CompareTo(Job o)
{
int returnVal;
Job temp = (Job)o;
if (this.totalFee > temp.totalFee)
returnVal = 1;
else
if (this.totalFee < temp.totalFee)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
我不能確定做什麼,從這裏排列jobs
當他們打印出total fees
。
我認爲你做得對。現在只需將該類的對象添加到列表中並使用sort()方法。 –
你的代碼不會編譯。您的代碼中有許多與IComparable或排序無關的明顯錯誤。在嘗試做更復雜的事情之前,您應該先嚐試修復它。 – Vijay