SoFunction
Updated on 2024-11-17

How to Sort Dates and Times with Python

Python is a high-level interpreted programming language used by programmers all over the world. It is best known for object-oriented programming.

We can use Python in different areas of IT related to artificial intelligence, machine learning, web development, and data analytics. Another reason for Python's popularity and usefulness is that it has many built-in libraries and modules.

This article will teach us how to sort dates and times using Python. We will also learn about the datetime module and the sorted method.

Sorting Dates and Times in Python

One of the things we can do with Python is sort dates and times. In some cases, we may need to sort some given dates and times.

For example, if we have a list of different dates and times and we need to set them in ascending or descending order, we can use Python to sort them.

Date-Time Modules in Python

First, there should be a date and time to sort by. Python provides us with the datetime module to easily handle dates and times.

There are six main classes under the datetime module: date, time, datetime, timedelta, tzinfo, timezone.

sorted() method

sorted() is a built-in function in Python that we can use to sort the elements of a list. The syntax of the sorted() method is as follows.

sorted(iterable, key=key, reverse=reverse)

Iterable here means a sequence or iterator that we need to sort. It can be a tuple, list or dictionary.

key and reverse are optional values we can give to the sort function.

If we want to determine the sort order, the key is that we can execute a function to do it. The default value is none.

Inverse is a boolean value that considers true and false values. If we set the value to true, we will sort the elements in descending order, and if we set the value to false, we will sort the elements in ascending order.

The default value of reverse is false.

We can use this method to sort dates and times. Let's see how we can do this.

Sorting dates using the sorted() method

As a first step, we should import the datetime method from the datetime module, since we're working with dates and times.

from datetime import datetime

We can then define a list containing some random dates.

dateList = ["2022-10-07", "2022-10-04", "2022-10-31", "2022-10-01"]

Here we have added dates with the same year and month but with different dates. Let's try to sort them and get the output.

print(sorted(dateList))

In the above statement, we have theprint() The function uses thesorted() method. In addition, we can assign the method to a variable and print it.

sortedDateList = sorted(dateList)
print(sortedDateList)

Both statements give us the same output. For this example, we use the first method.

Full Code:

from datetime import datetime
dateList = ["2022-10-07", "2022-10-04", "2022-10-31", "2022-10-01"]
print(sorted(dateList))

Output.

sorted date

As you can see, the dates have been sorted in ascending order.

Let's sort them in descending order by adding the reverse attribute with a value of true.

print(sorted(dateList, reverse=True))

After running the code, we will get the result as shown below.

As shown above, we can sort the dates in descending order.

Now let's try again with a different year and month.

dateList = ["2022-10-07", "2021-10-07", "2021-09-07", "2020-10-07", "2020-10-01"]

We can then sort and print them as we did before.

print(sorted(dateList))

Now we will get the result as shown below.

Sorting time with the sorted() method

Earlier, we tried to sort dates. Now let's try to sort different times using this method.

Let's import the datetime method from the datetime module.

from datetime import datetime

Then we can make a list of different times.

timeList = ["14:00:00", "02:00:00", "10:00:00", "23:00:00", "05:00:00"]

Now let's try to sort and print them.

print(sorted(timeList))

Output.

sorted time

As output, we will get the list of times in ascending order. As we did before, we can also sort the times in descending order.

print("\n", sorted(timeList, reverse=True))

Output.

sorted time descending

As shown below, let's change the time to different minutes and seconds.

timeList = ["14:03:29", "02:24:23", "10:02:59", "02:23:24", "10:03:00"]
print(sorted(timeList))

When we run the code, we will get the following expected result.

Sorting dates and times with the sorted() method

In the previous steps, we sorted date and time separately. Now let's try to sort date and time together.

Let's create a new list as dateTimeList. Then we can add some dates and different times as shown in the following code block.

dateTimeList = ["2022-10-07 14:03:29", "2022-10-08 02:01:23", "2022-10-07 10:02:59", "2022-10-07 02:01:24"]

As you can see, there are four dates and different times. Let's see if we can get the sorted list by this method.

print(sorted(dateTimeList))

Output.

sorted datetime

As shown above, we will get a sorted list of dates and times.

summarize

In this post, we learn a Python technique for sorting dates and times with thesorted() Methods.

As a first step, we should import the datetime module and then, we should also import the datetime method. Only then will we be able to use dates and times.

utilizationsorted() method, we created lists with different dates and times and sorted them as an example to understand the concept. There are other ways to sort dates and times, but this is a simple one that Python provides for us.

This article on the Python date and time sorting article is introduced to this, more related Python date and time sorting content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!