views:

173

answers:

7

I have a class called Order which has properties as OrderId,OrderDate,Quantity,Total. I have a List of this "Order" class.

List<Order> objListOrder=new List<Order> ();
objListOrder=GetOrderList();  // method to fill list of orders

Now i want to sort the list based on one property of the Order object(Ex :i need to sort by the order date/ order id)

How can i do this in C# ?

Thanks in advance

+1  A: 

Using LINQ

objListOrder = GetOrderList()
                   .OrderBy(o => o.OrderDate)
                   .ToList();

objListOrder = GetOrderList()
                   .OrderBy(o => o.OrderId)
                   .ToList();
Daniel A. White
+1  A: 

Make use of LiNQ OrderBy

List<Order> objListOrder=new List<Order> ();
    objListOrder=GetOrderList().OrderBy(o=>o.orderid).ToList();
Pranay Rana
+6  A: 

The easiest way I can think of it to use Linq:

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
Lazarus
+6  A: 

If you need to sort the list in-place then you can use the Sort method, passing a Comparison<T> delegate:

objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate));

If you prefer to create a new, sorted sequence rather than sort in-place then you can use LINQ's OrderBy method, as mentioned in the other answers.

LukeH
+3  A: 

If you want something like

ORDER BY OrderDate, OrderId

Then try something like following.

  List<Order> objListOrder = 
    source.OrderBy(order => order.OrderDate).ThenBy(order => order.OrderId).ToList();
Prakash Kalakoti
+5  A: 

To do this without LINQ on .Net2.0:

List<Order> 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<Order>
{
    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<Order> comparer = new MyOrderingClass();
orderList.Sort(comparer);
GenericTypeTea
+1 Great answer
GalacticCowboy
Oh, don't make me blush.
GenericTypeTea
+1  A: 

Doing it without Linq as you said:

public class Order : IComparable
{
    public DateTime OrderDate { get; set; }
    public int OrderId { get; set; }

    public int CompareTo(object obj)
    {
        Order orderToCompare = obj as Order;
        if (orderToCompare.OrderDate < OrderDate)
        {
            return 1;
        }
        if (orderToCompare.OrderDate > OrderDate)
        {
            return -1;
        }
        if (orderToCompare.OrderId > OrderId)
        {
            return -1;
        }
        if (orderToCompare.OrderId < OrderId)
        {
            return 1;
        }

        return 0;
    }
}

Then just call .sort() on your list of Orders

Jimmy Hoffa