489

How can I sort this list in descending order?

timestamps = [
    "2010-04-20 10:07:30",
    "2010-04-20 10:07:38",
    "2010-04-20 10:07:52",
    "2010-04-20 10:08:22",
    "2010-04-20 10:08:22",
    "2010-04-20 10:09:46",
    "2010-04-20 10:10:37",
    "2010-04-20 10:10:58",
    "2010-04-20 10:11:50",
    "2010-04-20 10:12:13",
    "2010-04-20 10:12:13",
    "2010-04-20 10:25:38"
]
0

7 Answers 7

602

This will give you a sorted version of the array.

sorted(timestamps, reverse=True)

If you want to sort in-place:

timestamps.sort(reverse=True)

Check the docs at Sorting HOW TO

Sign up to request clarification or add additional context in comments.

2 Comments

reverse was added in 2.4. But note that sort() is stable, so the two bits of code given won't necessarily give the same result.
@Rajeev - don't forget you can sort dates only if they are written in this way (YYYY-MM-DD HH:MM:SS), where alphabetically is the same like chronologically. 'DD.MM.YYYY' would be a good example, where you would need more than just sort(reverse=True).
430

In one line, using a lambda:

timestamps.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)

Passing a function to list.sort:

def foo(x):
    return time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6]

timestamps.sort(key=foo, reverse=True)

3 Comments

The conversion to a time tuple is unnecessary.
@Marcelo: Only by coincidence.
@IgnacioVazquez-Abrams no, not by coincidence. ISO 8601 is expressly designed so that alphabetical order coincides with chronological order.
67

You can simply do this:

timestamps.sort(reverse=True)

Comments

14

you simple type:

timestamps.sort()
timestamps=timestamps[::-1]

1 Comment

This is a strange answer because you do the sorting in-place but then the reversing out-of-place. If there is another variable aliasing the original list, its value afterwards will not have the elements in their original order, nor in descending order; the alias will point at a list sorted in ascending order. That could be rather surprising, and a source of subtle bugs.
11

Since your list is already in ascending order, we can simply reverse the list.

>>> timestamps.reverse()
>>> timestamps
['2010-04-20 10:25:38', 
'2010-04-20 10:12:13', 
'2010-04-20 10:12:13', 
'2010-04-20 10:11:50', 
'2010-04-20 10:10:58', 
'2010-04-20 10:10:37', 
'2010-04-20 10:09:46', 
'2010-04-20 10:08:22',
'2010-04-20 10:08:22', 
'2010-04-20 10:07:52', 
'2010-04-20 10:07:38', 
'2010-04-20 10:07:30']

Comments

6

Here is another way


timestamps.sort()
timestamps.reverse()
print(timestamps)

1 Comment

Never knew about reverse(), super useful
4

Especially if the data is numeric, negation can be used to sort in descending order. This is especially useful if you need to pass a sorting key anyway. For example, if the data was as follows:

data = ['9', '10', '3', '4.5']
sorted(data, reverse=True)                      # doesn't sort correctly
sorted(data, key=lambda x: -float(x))           # sorts correctly
#                          ^ negate here

# that said, passing a key along with reverse=True also work
sorted(data, key=float, reverse=True)           # ['10', '9', '4.5', '3']

For an example with datetime, that would look like as follows:

from datetime import datetime
ts = ["04/20/2010 10:07:30", "12/01/2009 10:07:52", "01/13/2020 10:08:22", "12/01/2009 12:07:52"]
ts.sort(key=lambda x: -datetime.strptime(x, '%m/%d/%Y %H:%M:%S').timestamp())
#                                                               ^^^^ convert to a number here
ts
# ['01/13/2020 10:08:22', '04/20/2010 10:07:30', '12/01/2009 12:07:52', '12/01/2009 10:07:52']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.