Sorting a list of string dates with no year but with 29th Feb

Viewed 506

I have a list of dates that are without a year component, which I need to sort. My code to sort them fails for 29th Feb. and raises a ValueError: day is out of range for month exception instead:

from datetime import datetime
a = [
    '7-Mar 11:20:33', '7-Mar 10:30:22','22-Mar 22:33:11', '31-Mar 11:22:33', 
    '25-Mar 03:04:05', '1-Jan 00:00:00', '29-Feb 02:11:10'
]
a.sort(key=lambda date: datetime.strptime(date, "%d-%b %H:%M:%S"))

Is there any way to sort these dates with default leap year?

After sorting, the list should be:

['1-Jan 00:00:00', '29-Feb 02:11:10', '7-Mar 10:30:22', '7-Mar 11:20:33',
 '22-Mar 22:33:11', '25-Mar 03:04:05', '31-Mar 11:22:33']
1 Answers
Related