c# - 用于对不同属性中的不同对象进行排序的通用 IComparer

标签 c# arrays sorting icomparable icomparer

我正在尝试使用 IComparer 对对象数组进行排序。

我编写了代码,但它仅适用于特定对象。例如:

这门课

public class Cars
{
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    public int Year { get; set; }

    public Cars(string name, string manufacturer, int year)
    {
        Name = name;
        Manufacturer = manufacturer;
        Year = year;
    }
}

我的代码如下:

class MySort 
{
    public class SortByYears : IComparer
    {
        int IComparer.Compare(Object x, Object y)
        {
            Cars X = (Cars)x, Y = (Cars)y;                
            return (X.Year.CompareTo(Y.Year));
        }
    }

    public class SortByName : IComparer
    {
        int IComparer.Compare(Object x, object y)
        {
            Cars X = (Cars)x, Y = (Cars)y;
            return (X.Name.CompareTo(Y.Name));
        }
    }

    public class SortByManyfacturer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            Cars X = (Cars)x, Y = (Cars)y;
            return (X.Manufacturer.CompareTo(Y.Manufacturer));
        }
    }
}   

但是如果我添加另一个具有不同属性的类,它将毫无用处。

那么是否有机会修改这段代码,使其适用于具有不同属性的对象?

最佳答案

class SortComparer<T> : IComparer<T>
{
   private PropertyDescriptor PropDesc = null;
   private ListSortDirection Direction =
      ListSortDirection.Ascending;

   public SortComparer(object item,string property,ListSortDirection direction)
   {
       PropDesc = TypeDescriptor.GetProperties(item)[property];
       Direction = direction;
   }

   int IComparer<T>.Compare(T x, T y)
   {    
      object xValue = PropDesc.GetValue(x);
      object yValue = PropDesc.GetValue(y);
      return CompareValues(xValue, yValue, Direction);
   }

   private int CompareValues(object xValue, object yValue,ListSortDirection direction)
   {

      int retValue = 0;
      if (xValue is IComparable) // Can ask the x value
      {
         retValue = ((IComparable)xValue).CompareTo(yValue);
      }
      else if (yValue is IComparable) //Can ask the y value
      {
         retValue = ((IComparable)yValue).CompareTo(xValue);
      }
      // not comparable, compare String representations
      else if (!xValue.Equals(yValue))
      {
         retValue = xValue.ToString().CompareTo(yValue.ToString());
      }
      if (direction == ListSortDirection.Ascending)
      {
         return retValue;
      }
      else
      {
         return retValue * -1;
      }
   }
}

调用代码:

假设一个名为 lst 的列表:

lst.Sort(new SortComparer<Cars>(lst[0],"YourPropertyName",ListSortDirection.Ascending));

关于c# - 用于对不同属性中的不同对象进行排序的通用 IComparer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074893/

相关文章:

scala - 如何使用我的相等比较器对 Spark DataFrame 进行 GroupBy?

c++ - 流行的 C++ 编译器对 std::sort 和 std::stable_sort 使用什么算法?

java - 为什么我不能在这段代码中使用 Collections.max() 函数? - java

c# - 跳过文本文件中包含分号的行

c# - mvc3登录表单错误的用户名和密码消息

c# - 类的隐式逆变 - 简单的失败示例

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

C# 获取非托管 dll 的版本

c++ - 分配给 map 时构造的两个对象

python - 如何将数组复制/重复 N 次到新数组中?