How to Sort a List by a property in the object

前端 未结 20 1979
醉梦人生
醉梦人生 2020-11-21 08:25

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a l

20条回答
  •  野性不改
    2020-11-21 08:43

    To do this without LINQ on .Net2.0:

    List objListOrder = GetOrderList();
    objListOrder.Sort(
        delegate(Order p1, Order p2)
        {
            return p1.OrderDate.CompareTo(p2.OrderDate);
        }
    );
    

    If you're on .Net3.0, then LukeH's answer is what you're after.

    To sort on multiple properties, you can still do it within a delegate. For example:

    orderList.Sort(
        delegate(Order p1, Order p2)
        {
            int compareDate = p1.Date.CompareTo(p2.Date);
            if (compareDate == 0)
            {
                return p2.OrderID.CompareTo(p1.OrderID);
            }
            return compareDate;
        }
    );
    

    This would give you ascending dates with descending orderIds.

    However, I wouldn't recommend sticking delegates as it will mean lots of places without code re-use. You should implement an IComparer and just pass that through to your Sort method. See here.

    public class MyOrderingClass : IComparer
    {
        public int Compare(Order x, Order y)
        {
            int compareDate = x.Date.CompareTo(y.Date);
            if (compareDate == 0)
            {
                return x.OrderID.CompareTo(y.OrderID);
            }
            return compareDate;
        }
    }
    

    And then to use this IComparer class, just instantiate it and pass it to your Sort method:

    IComparer comparer = new MyOrderingClass();
    orderList.Sort(comparer);
    

提交回复
热议问题