1

所以我有一堂课

Class MyClass{
    string myString;

    MyClass(string newString){
    myString = newString;
    }
}

我有这些的清单

List<MyClass> myList = new List<MyClass>();
myList.Add(new MyClass(some string...);

我可以使用 List.Sort 根据类字段的值对对象进行排序吗?如果是这样怎么办?

谢谢

4

2 回答 2

4

您可以使用Enumerable.OrderBy

myList = myList.OrderBy(x => x.myString).ToList();

但请注意,这会基于旧列表创建一个新列表。在许多情况下,这不会产生影响,但需要注意这一点。


或者,由于您仅限于 .NET 2.0,您可以定义自定义函数进行比较

class MyClass
{
    string myString;

    ...

    public static int CompareByMyString(MyClass x, MyClass y)
    {
        return String.Compare(x.myString, y.myString);
    }
}

并将其提供给List.Sort

myList.Sort(MyClass.CompareByMyString);
于 2013-03-06T14:34:00.303 回答
0

您可以从 IComparable 继承。这方面的一个例子是:

public class MyClass : IComparable
    {
        public int Day { get; set; }
        public int Hour { get; set; }

        public int CompareTo(object obj)
        {
            if (obj is MyClass)
            {
                MyClass slot = (MyClass)obj;

                if (Day < slot.Day)
                {
                    return -1;
                }
                else if (Day > slot.Day)
                {
                    return 1;
                }
                else
                {
                    if (Hour < slot.Hour)
                    {
                        return -1;
                    }
                    else if (Hour > slot.Hour)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            else
            {
                throw new ArgumentException("Object is not MyClass");
            }
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return Day.GetHashCode() ^ Hour.GetHashCode();
            }
        }

        public override bool Equals(object obj)
        {
            if (obj is MyClass)
            {
                return this == (MyClass)obj;
            }
            else
            {
                throw new ArgumentException("Object is not MyClass");
            }
        }

        public static bool operator ==(MyClass a, MyClass b)
        {
            return a.CompareTo(b) == 0;
        }

        public static bool operator !=(MyClass a, MyClass b)
        {
            return a.CompareTo(b) != 0;
        }

        public static bool operator <(MyClass a, MyClass b)
        {
            return a.CompareTo(b) < 0;
        }

        public static bool operator >(MyClass a, MyClass b)
        {
            return a.CompareTo(b) > 0;
        }

        public static bool operator <=(MyClass a, MyClass b)
        {
            return a.CompareTo(b) <= 0;
        }

        public static bool operator >=(MyClass a, MyClass b)
        {
            return a.CompareTo(b) >= 0;
        }
    }

然后你可以这样做:

List<MyClass> listOfStuff = new List<MyClass>();}
// Add some stuff to the list E.G. 
listOfStuff.add(new MyClass());
// Sort the list
listOfStuff.sort();
于 2013-03-06T14:57:10.717 回答