Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 289
user2138160 Avatar asked Aug 06 '13 01:08

user2138160


People also ask

How do I order a datetime list?

Use sorted() to sort a list of datetime objects. Call sorted(date_list) with date_list as a list of datetime. datetime objects to return a new list with the elements of date_list in increasing order.

How do you sort dates in ascending order in Python?

Given a list of dates in string format, write a Python program to sort the list of dates in ascending order. Approach: In Python, we can use sort() (for in-place sorting) and sorted() (returns a new sorted list) functions for sorting lists.


1 Answers

Simply, OrderBy the TimeOfDay:

var list = dateList.OrderBy(x => x.TimeOfDay).ToList(); 
// ToList added in response to comment.
like image 89
Simon Whitehead Avatar answered Sep 19 '22 12:09

Simon Whitehead