Take the 2-minute tour ×

In c# (3.0 or 3.5, so we can use lambdas), is there an elegant way of sorting a list of dates in descending order? I know I can do a straight sort and then reverse the whole thing,

docs.Sort((x, y) => x.StoredDate.CompareTo(y.StoredDate));

but is there a lambda expression to do it one step?

In the above example, StoredDate is a property typed as a DateTime.

share|improve this question

4 Answers 4

up vote 46 down vote accepted

Though it's untested...

docs.Sort((x, y) => y.StoredDate.CompareTo(x.StoredDate));

should be the opposite of what you originally had.

share|improve this answer

What's wrong with

docs.OrderByDescending(d => d.StoredDate)?
share|improve this answer
Works with me :) –  AshOoO Apr 7 '12 at 8:14
+1 for brevity. –  Kornelije Petak Jun 6 '12 at 10:50
Note that this creates a new ordered list rather than modifying the list in place. Granted, the performance loss is negligible. –  Cavyn VonDeylen Oct 19 '12 at 15:01
Adding to what Cavyn said, this will always returns list of IOrderedEnumerable<T> which would need to be converted back to generic list using .ToList<T>(). –  digitguy Dec 11 '12 at 10:12
docs.Sort((x, y) => -x.StoredDate.CompareTo(y.StoredDate));

Note the minus sign.

share|improve this answer
Saved my life :D –  Mario Galván Jul 28 at 18:06

Should do what you're looking for.

share|improve this answer

Your Answer


