Python Sorting date

One of the best ways to sort a group of dates is to store them into a list and then apply list's sort() method.This will sort all the dates which are available in the list.We can store the date class objects into the list using append() method as:

list.append(d)

Here, 'd' represents the date class object that is being appended to the list 'list'.

Python program to sorting date example

from datetime import * 
#take an empty list
group = [] 
#add today's date to list, i.e. 2016-04-30 
group.append(date.today()) 
#create some more dates and add them to list 
d = date(2015, 6, 29) 
group.append(d) 
d = date(2014, 6, 30) 
group.append(d) 
#add 25 days to the date and add to list 
group.append(d+timedelta(days=25)) 
#sort the list 
group.sort() #display sorted dates 
for d in group: 
   print(d)