Sort List of datetime Objects in Python (Example)

 

In this tutorial, you’ll learn how to sort a list of datetime objects using the Python programming language.

The tutorial structure can be seen below:

Let’s get straight into it!

 

Module Import & Data Initialization

Firstly, we need to import datetime from the datetime module.

from datetime import datetime
 
dt1 = datetime(2015, 10, 24, 12, 36, 9)
dt2 = datetime(2015, 11, 24, 12, 36, 9)
dt3 = datetime(1900, 7, 18)
dt4 = datetime(2014, 5, 27)

Furthermore, we have to create a list of datetime objects:

dt_list = [dt1, dt2, dt3, dt4]
print(dt_list)
# [datetime.datetime(2015, 10, 24, 12, 36, 9),
# datetime.datetime(2015, 11, 24, 12, 36, 9),
# datetime.datetime(1900, 7, 18, 0, 0),
# datetime.datetime(2014, 5, 27, 0, 0)]

Datetime objects can be compared with the comparison operators, thus sorting a list of datetime objects is no different from sorting a list of integers.

 

Example 1: Defining & Implementing a Sorting Algorithm of Your Own

In Python, we use comparison operators ‘<‘, ‘>’, ‘=’ to compare integer quantities with each other.

These operators can also be used with datetime objects, and by using them we can sort a list of datetime objects just as we would sort a list of integers.

We can define and utilize any sorting algorithm. In this example, we will use the implementation of the selectionSort algorithm.

def selectionSort(arr):
    for x in range(len(arr)):
        min_idx = x
 
        for i in range(x+1, len(arr)):
            if arr[i] < arr[min_idx]:
                min_idx = i
 
        arr[x], arr[min_idx] = arr[min_idx], arr[x]
 
    return arr

We will copy the list to leave the list unsorted for the next example as well.

copy_list = dt_list.copy()
print(selectionSort(copy_list))
# [datetime.datetime(1900, 7, 18, 0, 0),
# datetime.datetime(2014, 5, 27, 0, 0),
# datetime.datetime(2015, 10, 24, 12, 36, 9),
# datetime.datetime(2015, 11, 24, 12, 36, 9)]

The output is the sorted version of the list in increasing order.

 

Example 2: Using the Built-in sort() Function

If you don’t want to implement your own sorting algorithm, you can use the built-in sorting algorithm.

This function utilizes a combination of MergeSort and InsertionSort. The function also has a parameter named reverse, which can be used to select the order of the list.

If it is set to “False”, then the list is sorted in increasing order, and it’s vice versa for it’s “True” setting.

dt_list.sort(reverse=False)
print(dt_list)
# [datetime.datetime(1900, 7, 18, 0, 0),
# datetime.datetime(2014, 5, 27, 0, 0),
# datetime.datetime(2015, 10, 24, 12, 36, 9),
# datetime.datetime(2015, 11, 24, 12, 36, 9)]

 

Video, Further Resources & Summary

Do you need more explanations on how to sort a list of datetime objects in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to sort a list of datetime objects. In case you have further questions, you may leave a comment below.

 

Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top