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.

Sort Date

We can store the date class objects into the list using append() method as:


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

Sorting date example

CopiedCopy Code

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