从通用容器中对通用列表进行排序 class

sorting a generic list from within a generic container class

我需要按列表的任何一个属性对列表进行排序,但我不知道它将具体根据哪些属性进行排序。下面是 Main 方法。

  static void Main(string[] args)
    {
        Things<Something> something = new Things<Something>();
        something.Add(new Something 
                        { Thing = "Apartment", Price = 1500000 });
        something.Add(new Something
                        { Thing = "Bed", Price = 10000 });
        something.Add(new Something 
                        { Thing = "Lamp", Price = 600 });
        something.Add(new Something 
                        { Thing = "Car", Price = 5000000 });

        Console.WriteLine("\n\tStuff sorted by description");
        something = something.SelectionSort("Thing");
        foreach (Something thing in something)
            Console.WriteLine("\t" + thing);

        Console.WriteLine("\n\tStock items sorted by value");
        something = something.SelectionSort("Value");
        foreach (Something thing in something)
            Console.WriteLine("\t" + thing);

        Console.Write("\n\tPress any key to exit ...");
        Console.ReadKey();
    } 

我有一个结构

  public struct Something
 {
  public string Thing { get; set; }
  public decimal Price { get; set; }
 }

还有一个名为 things

的通用容器 class
     public class Things<T> : IEnumerable<T>
{
    
    private List<T> lstItems;
    public int Count { get { return lstItems.Count; } }


    public Things() { lstItems = new List<T>(); }
    public Things(List<T> items_) { lstItems = new List<T>(items_); }

    public void Add(T item)
    {
        lstItems.Add(item);
    }

    
    public T this[int i]
    {
        get { return lstItems[i]; }
        set { lstItems[i] = value; }
    }
    
    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new System.NotImplementedException();
    }

    public IEnumerator<T> GetEnumerator()
    {
        foreach (T item in lstItems)
            yield return item;
    }  
}

扩展 class 扩展通用容器 class

 public static class ExtensionsClass
{
    private static string SortFiield { get; set; }

    private static object GetProperty<T>(T thing, string nameofProp)
    {
        return thing.GetType().GetProperty(nameofProp).GetValue(thing, null);
    }

    private static int Compare<T>(T x, T y)
    {
        IComparable propX = (IComparable)GetProperty(x, SortFiield);
        IComparable propY = (IComparable)GetProperty(y, SortFiield);
        return propX.CompareTo(propY);
    }

    public static Things<T> SelectionSort<T>(this Things<T> things, string SORTFIELD)
    {
        List<T> lsstt = new List<T>(things);
        int iIndex;
        T temporary;
        SortFiield = SORTFIELD;
        for (int i = 0; i < lsstt.Count - 1; i++)
        {
            iIndex = i;
            for (int j = i + 1; j < lsstt.Count; j++)
            {
                string first = GetProperty(lsstt[j], SortFiield).ToString();
                string second = GetProperty(lsstt[iIndex], SortFiield).ToString();

                if (Compare(first, second) < 0)
                    iIndex = j;
            }
            temporary = lsstt[i];
            lsstt[i] = lsstt[iIndex];
            lsstt[iIndex] = temporary;
        }
        return new Things<T>(lsstt);
    }
}  

我遇到的问题是扩展 class return 中的 属性 为空,但我知道我正在尝试 return 的对象存在.它由“String first = ....”行找到,但是当从 Compare 方法调用 get属性 时,它 returns null.

您正在传递“第一个”,“第二个" 进行比较。在您的情况下,它们都是字符串而不是对象,您需要传递“lsstt[j]”和“lsstt[iIndex]" 到它。

if (Compare(lsstt[j], lsstt[iIndex]) < 0)
    iIndex = j;