Python list sort in descending order
Solution 1:
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
Solution 2:
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)
Solution 3:
You can simply do this:
timestamps.sort(reverse=True)
Solution 4:
you simple type:
timestamps.sort()
timestamps=timestamps[::-1]