2014-02-24 37 views
1

比較,我有一個類,像這樣:IComparable的的CompareTo(),我如何能實現在2個不同的領域

public class Incident : IComparable<Incident> 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
    public string IncidentType { get; set; } 

    public int CompareTo(Incident other) 
    { 
     string str = other.Description; 
     int ret = -1; 
     if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) < 0) 
      ret = 1; 
     else if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) > 0) 
      ret = -1; 
     else if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) == 0) 
      ret = 0; 

     return ret; 
    } 
} 

我可以按字母基礎上,描述字段這個類的對象。 如何根據IncidentType字段對類進行排序?

我不想在兩個字段上同時對它們進行排序。 有時候我想通過事件Description,有時IncidentType

+0

你在用什麼排序? 'List.Sort()'? '的OrderBy()'?還有別的嗎? – svick

+0

使用OrderBy()進行排序。 – theIrishUser

+1

與您的問題無關,但是有幫助的提示。通過執行'int ret = String.Compare(Description,str,StringComparison.OrdinalIgnoreCase);',然後返回'ret',可以將比較次數從3減少到1。 'CompareTo'不會說返回值必須是'-1'或'0'或'1'。它表示它必須是「<0」,「0」或「> 0」。 –

回答

3

如果它是一個典型案例,爲您無論是DescriptionIncidentType排序,你可以實現不同Comparers進行排序不同條件

public class Incident: IComparable<Incident> { 
    ... 

    private IncidentTypeComparerClass: IComparer<Incident> { 
    public int Compare(Incident x, Incident y) { 
     if (Object.ReferenceEquals(x, y)) 
     return 0; 
     else if (Object.ReferenceEquals(x, null)) 
     return -1; 
     else if (Object.ReferenceEquals(null, y)) 
     return 1; 

     return String.Compare(x.IncidentType, y.IncidentType, StringComparison.OrdinalIgnoreCase); 
    }  
    } 

    // Additional comparer, by IncidentType 
    public static readonly ByIncidentTypeComparer: IComparer<Incident> = new IncidentTypeComparerClass(); 

    ... 
} 

... 

List<Incident> incidents = ... 

// Sort by IncidentType  
incidents.Sort(Incident.ByIncidentTypeComparer); 
... 
// Default sort (that's by Description) 
incidents.Sort(); 

如果你想IncidentType排序只一次或兩次,你可以做到這一點明確

List<Incident> incidents = ... 

incidents.Sort((x, y) => String.Compare(x.IncidentType, 
             y.IncidentType, 
             StringComparison.OrdinalIgnoreCase)); 
0
public int CompareTo(Incident other) 
    { 
     string str = other.IncidentType; 
     int ret = -1; 
     if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) < 0) 
      ret = 1; 
     else if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) > 0) 
      ret = -1; 
     else if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) == 0) 
      ret = 0; 

     return ret; 
    } 

記住排序,這只是在IncidentType一個普通的字符串比較。您可能需要在此處引入更復雜的業務規則。

3

可以實現獨立的類IComparer<T>當你整理你的內容,你可以使用這個比較器:

public class IncidentComparer : IComparer<Incident> 
{ 
    public int Compare(Incident x, Incident y) 
    { 
     return x.IncidentType.CompareTo(y.IncidentType); 
    } 
} 

例如,如果你有這樣的列表,你可以用你的比較器:

List<Incident> incidents = new List<Incident>(); 
... 
incidents.Sort(new IncidentComparer()); 

或者您可以使用LINQ代替:

incidents = incidents.OrderBy(x => x.IncidentType).ToList(); 
+2

+1。另一種選擇是提供[比較代表](http://msdn.microsoft.com/en-us/library/tfakywbh(v = vs.110).aspx)。正如在'incidents.Sort((x,y)=> x.IncidentType.CompareTo(y.IncidentType));' –

1

您可以返回一個比函數結尾更早的值,因此如果ret爲0,您可以讓if語句檢查,如果爲真,則返回ret。如果不是這樣,if語句將被跳過,並且函數的其餘部分將被評估。例如:

public class Incident : IComparable<Incident> 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
    public string IncidentType { get; set; } 

    public int CompareTo(Incident other) 
    { 
     string str = other.Description; 
     int ret = String.Compare(str, Description, StringComparison.OrdinalIgnoreCase); 
     if (ret != 0) 
      return ret; 
     str = other.IncidentType; 
     ret = String.Compare(str, IncidentType, StringComparison.OrdinalIgnoreCase); 
     return ret; 
    } 
} 

將按說明進行排序,如果說明相同,則按IncidentType進行排序。如果您想先通過IncidentType進行排序,請將其切換。

+1

不要使用'-',而是交換參數。 '-int.MinValue'仍然是負值。 – CodesInChaos

+0

編輯以反映評論 – user3288049

+0

您誤讀了這個問題:「我不想在兩個字段上同時對它們進行排序,有時候我想通過'Description'對事件進行排序,有時在'IncidentType'上。」 – CodesInChaos

0

要排序,如果你有例如:

List<Indicent> listIncident = new List<Incident>(); 

//This to sort based on the incidentType 
listIndicent = listIncident.OrderBy(x => x.IncidentType).ToList(); 

//This to sort based on the description 
listIndicent = listIncident.OrderBy(x => x.Description).ToList(); 

//This to sort based on two 
listIndicent = listIncident.OrderBy(x => x.Description).ThenBy(x => x.IncidentType).ToList();