Sometimes, we want to sort Python list sort in descending order.
In this article, we’ll look at how to sort Python list sort in descending order.
How to sort Python list sort in descending order?
To sort Python list sort in descending order, we can use the sorted function.
For instance, we write
timestamps = [
"2022-04-20 10:07:30",
"2022-04-20 10:07:38",
"2022-04-20 10:07:52",
"2022-04-20 10:08:22",
"2022-04-20 10:08:22",
"2022-04-20 10:09:46",
"2022-04-20 10:10:37"
]
sorted_t = sorted(timestamps, reverse=True)
to call sorted with the timestamps list with the reverse argument set to True to sort timestamps in reverse and return the new list.
Then we assign the returned sorted list to sorted_t.
Conclusion
To sort Python list sort in descending order, we can use the sorted function.