Search code examples
c#listsortingdatetimelambda

Sort DateTime List by Time


I have a datetime list and I would like to sort it using a lambda expression if possible.

My list:

6/19/1979 8:00:00 AM
5/5/1980 7:00:00 PM
10/20/1982 5:00:00 PM
1/4/1984 6:00:00 AM

The output should be in this order:

1/4/1984 6:00:00 AM 
6/19/1979 8:00:00 AM
10/20/1982 5:00:00 PM
5/5/1980 7:00:00 PM

Solution

  • Simply, OrderBy the TimeOfDay:

    var list = dateList.OrderBy(x => x.TimeOfDay).ToList(); 
    // ToList added in response to comment.