454

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"
]
cottontail
  • 10,268
  • 18
  • 50
  • 51
Rajeev
  • 44,985
  • 76
  • 186
  • 285

7 Answers7

560

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

Ricardo
  • 3,696
  • 5
  • 36
  • 50
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 2
    `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. – Ignacio Vazquez-Abrams Nov 15 '10 at 10:49
  • 6
    @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)`. – eumiro Nov 15 '10 at 11:28
426

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)
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
66

You can simply do this:

timestamps.sort(reverse=True)
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Wolph
  • 78,177
  • 11
  • 137
  • 148
13

you simple type:

timestamps.sort()
timestamps=timestamps[::-1]
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 3
    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. – kaya3 Nov 10 '19 at 16:44
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']
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
5

Here is another way


timestamps.sort()
timestamps.reverse()
print(timestamps)
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
hamdan
  • 203
  • 1
  • 5
  • 12
1

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']
cottontail
  • 10,268
  • 18
  • 50
  • 51