how to sort a collection by datetime in c#

You seem to be working with a List<T> object, in which case the most efficient (and a simple) method would be the following:

myList.Sort((x, y) => DateTime.Compare(x.Created, y.Created));

This uses the overload of the List.Sort method than takes a Comparison<T> delegate (and thus lambda expression).

You can of course use the LINQ OrderBy extension method, but I don’t think this offers any advantages, and can be significantly slower, depending on your situation.

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

Leave a Comment