Python list sort in descending order

Rajeev picture Rajeev · Nov 15, 2010 · Viewed 655.7k times · Source

How can I sort this list in descending order?

timestamp = [
    "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"
]

Answer

Marcelo Cantos picture Marcelo Cantos · Nov 15, 2010

This will give you a sorted version of the array.

sorted(timestamp, reverse=True)

If you want to sort in-place:

timestamp.sort(reverse=True)