Python list sort in descending order Python list sort in descending order python python

Python list sort in descending order


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)


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)


You can simply do this:

timestamps.sort(reverse=True)


matomo