Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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" ]

2 Answers

0 votes
by (106k points)

For sorting the list in descending order you can use a lambda function below is the code for the same:

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

Another way is to pass a function to list.sort:

def foo(x):

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

0 votes
by (20.3k points)

The code given below will give you a sorted version of the array.

sorted(timestamp, reverse=True)

And, if you want to sort in-place then use this:

timestamp.sort(reverse=True)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
0 votes
1 answer
asked Aug 1, 2019 in Python by Sammy (47.6k points)
0 votes
4 answers

Browse Categories

...