【问题标题】:IComparable CompareTo(), How do I compare multiple object properties?IComparable CompareTo(),如何比较多个对象属性?
【发布时间】:2017-10-14 06:35:40
【问题描述】:

我正在尝试比较对象的多个属性,但我的代码只能比较 degree 属性。在 Visual Studio 中调试时,我的代码似乎完全缺少 else 语句。如果有任何提示,我将不胜感激。

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。这可能不是你想要的。

标签: c#


【解决方案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);


   }
}

【讨论】:

    【解决方案2】:

    如果this.deg.CompareTo(newStudent.deg) == 1,则返回1,如果this.deg.CompareTo(newStudent.deg) != 1,则返回-1。由于比较要么等于 1,要么不等于 1,因此您永远无法达到其余的 else。

    因此您的代码应如下所示:

    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;
        }
    }
    

    注意事项:

    【讨论】:

      【解决方案3】:

      在您的代码中

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

      else 语句永远不会到达,因为结果可以等于或不等于 1。 而且您只检查“度”值。例如,您可以像这样检查它们是否相等:

      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;
          }
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2013-08-26
      • 1970-01-01
      • 2011-01-29
      相关资源
      最近更新 更多