how to sort the date list in python

Question:

I have a list in python as follows..

[Timestamp('2016-01-03 10:38:52'),
 Timestamp('2016-01-18 09:37:29'),
 Timestamp('2016-02-06 09:44:44'),
 Timestamp('2016-02-07 11:11:28'),
 Timestamp('2016-02-15 11:24:41'),
 Timestamp('2016-02-20 12:46:07'),
 Timestamp('2016-02-21 11:07:11')]

I want to sort this with ascending order

I tried with temp_list.sort() but it does not display any output

Asked By: Neil

||

Answers:

temp_list.sort() will sort the list in place. That means that it will not return anything. You can say x = sorted(y) to assign x to a sorted version of y, but you could also say y.sort() to define y as the sorted version.

Example with sort:

>>> xx = [2, 1]
>>> xx.sort()
>>> print(xx)
[1, 2]             # xx got sorted

and with sorted:

>>> xx = [2, 1]
>>> sorted_xx = sorted(my_list)
>>> print(sorted_xx)
[1, 2]
>>> print(xx)
[2, 1]             # xx is still unsorted
Answered By: zondo

Python sorts lists in place, meaning there is no return. If you were to look at the value of your list again you will see it sorted in ascending order.

Answered By: Wendy Moniuk
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.