2017-10-14 39 views
0

我想比較對象的多個屬性,但我的代碼只能比較度數屬性。在Visual Studio中調試時,看起來我的代碼完全缺少else語句。我會很感激任何提示。IComparable CompareTo(),如何比較多個對象屬性?

class Student : IComparable 
{ 
    private string fName; 
    private string lName; 
    private string deg; 
    private int gra; 

    public Student(string firstName, string lastName, string degree, int grade) 
    { 
     fName = firstName; 
     lName = lastName; 
     deg = degree; 
     gra = grade; 
    } 

    public override string ToString() 
    { 
     string var = lName + ", " + fName + " (" + deg + ") Grade: " + gra; 
     return var; 
    } 

    public int CompareTo(object obj) 
    { 
     Student newStudent = obj as Student; 

     if (this.deg.CompareTo(newStudent.deg) == 1) 
     { 
      return 1; 
     } 
     else if (this.deg.CompareTo(newStudent.deg) != 1) 
     { 
      return -1; 
     } 
     else //this is what my code is ignoring and not ordering by firstname as well 
     { 
      if (this.fName == newStudent.fName) 
      { 
       return 0; 
      } 
      else if (this.fName != newStudent.fName) 
      { 
       return -1; 
      } 
      else 
      { 
       return 0; 
      } 
     } 
    } 
} 
+1

您返回1,如果馬上度是相等的。這可能不是你想要的。 – 2017-10-14 06:37:18

回答

0

在你的代碼

if (this.deg.CompareTo(newStudent.deg) == 1) 
{ 
    // do something 
} 
else if (this.deg.CompareTo(newStudent.deg) != 1) 
{ 
    // do something 
} 
else 
{ 
    // do something 
} 

else語句永遠不會被達到,因爲結果可以是等於1或不。 而你只檢查'deg'值。例如,您可以檢查它們是否相等這樣的:

public int CompareTo(object obj) 
{ 
    if (obj == null) 
    { 
     return -1; 
    } 
    Student newStudent = obj as Student; 
    // are equal 
    if (deg.CompareTo(newStudent.deg) == 0 && 
     gra.CompareTo(newStudent.gra) == 0 && 
     lName.CompareTo(newStudent.lName) == 0 && 
     fName.CompareTo(newStudent.fName) == 0) 
    { 
     return 0; 
    } 
    else 
    { 
     return 1; 
    } 
} 
0

您返回1,如果this.deg.CompareTo(newStudent.deg) == 1和返回-1 this.deg.CompareTo(newStudent.deg) != 1。由於比較結果不是等於1,所以你永遠不會達到剩下的其他結果。

因此你的代碼看起來應該像下面這樣:

partial class Student : IComparable, IComparable<Student> 
{ 
    public int CompareTo(object obj) 
    { 
     if (obj != null && obj.GetType() != GetType()) 
     { 
      // From https://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx#Remarks 
      // The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown. 
      throw new ArgumentException(string.Format("Object must be of type {0}", GetType())); 
     } 
     return CompareTo((Student)obj); 
    } 

    public int CompareTo(Student newStudent) 
    { 
     if (object.ReferenceEquals(this, newStudent)) 
      return 0; 
     else if (newStudent == null) 
      // From https://msdn.microsoft.com/en-us/library/43hc6wht(v=vs.110).aspx#Remarks 
      // By definition, any object compares greater than null, and two null references compare equal to each other. 
      return 1; 

     var cmp = this.deg.CompareTo(newStudent.deg); 
     if (cmp != 0) 
      return cmp; 

     cmp = this.fName.CompareTo(newStudent.fName); 
     if (cmp != 0) 
      return cmp; 

     // Compare additional members as required, return the first nonzero member comparison. 

     // Finally return 0 if all member comparisons returned 0. 
     return 0; 
    } 
} 

注:

1

首先回答你的問題(爲什麼else部分從未被執行)已經回答了,那就是,有其是1not 1的條件沒有第三種可能。

如果您正在試圖通過不同的屬性和例如排序,如果首先我們想度進行排序,然後通過fName 那麼我們就可以實現IComparer-

class Student : IComparer<Student>{ 

    /* 
    codes 
    */ 


    public int Compare(Student student1, Student student2) 
    { 
      if(student1.deg.Equals(student2.deg)) //if both degrees are same 
      { 
       return string.Compare(student1.fName , student2.fName); // then compare fName 
      } 
      else 
       return string.Compare(student1.deg , student2.deg); 


    } 
}