tags:

views:

167

answers:

4

Let's say I have a list of objects, and I want to sort it by the items DateModified property. Why can't I use a delegate like this? How should I sort these by DateModified if not as shown below:

public string SortByDateModified(List<CartItem> items)
{
    items.Sort(new Func<CartItem, CartItem, bool>((itemA, itemB) =>
    {
        return itemA.DateModified < itemB.DateModified;
    }));
}
+5  A: 
public string SortByDateModified(List<CartItem> items) 
{ 
    items.Sort(delegate(CartItem itemA, CarItem itemB) 
    { 
        return itemA.DateModified.CompareTo(itemB.DateModified); 
    }); 
} 
John JJ Curtis
1+ but voting limit reached :(
Skurmedel
A: 

bool is not useful in such a delegate, usually int is used because you need 3 values to represent the results of the comparison, less than, equal and greater than. .NET collections usually (if not always) assume -1 means less than, 0 means equal and 1 means greater than.

You would then, in your delegate, have to check if value x is less, equal or greater than value y. An interesting thing to note here is that if you flip the results, for example compare y with x instead you will sort in the opposite direction.

For the easiest way to sort the dates, check JohnC's answer, or Sam's.

Skurmedel
+9  A: 

Why not use a lambda expression?

public string SortByDateModified(List<CartItem> items) 
{ 
    items.Sort((a, b) => a.DateModified.CompareTo(b.DateModified)); 
} 
Sam
Definitely. Jeff's answer was *correct*, but *yuck*. A lambda is so much cleaner.
Aaronaught
One small note, you missed a `)` :)
Skurmedel
Skurmedel, thanks, I edited the answer.
Sam
This is with caveat - you must be targeting .Net 3.5 or greater. If you are targeting 2.0 for compatibility reasons, the delegate answer must be used.
Mike S
+2  A: 

The Sort method takes a delegate called Comparison<T>. You're trying to pass in a Func<int, int, bool>, which is itself a delegate. There is no conversion between the delegate Func<int, int, bool> and the delegate Comparison<T>.

You can, however, use a lambda expression.

items.Sort((a, b) => a.DateModified.CompareTo(b.DateModified));

Indeed, you use this very lambda expression and pass it into the Func<int, int, bool> constructor*. However, there is no need. A lambda expression can be converted into any delegate whos signature matches - that is (a, b) => a.DateModified.CompareTo(b.DateModified) can be assigned to something typed Func<int, int, int> or something typed Comparison<T>. In this case we pass it in to something which expects a Comparison<T>.

* With one minor adjustment. Sort expectes an integer as a return type. Negative values indicate less than, 0 indicates equal, and positive values indicate greater than.

ICR