Python: Sort List By Date?
Is it possible to sort this list by date? It is for a flot graph so it has to be organized in this format of a list of smaller list pairs. I would like to be able to sort it by dat
Solution 1:
Here is one way:
In [9]: l = [["2014-5-29", 19], ["2014-5-28", 16], ["2014-5-30", 20], ["2014-5-23", 16], ["2014-5-22", 1225], ["2014-5-21", 114], ["2014-5-20", 69], ["2014-5-27", 10], ["2014-5-31", 17], ["2014-5-25", 18], ["2014-5-24", 19], ["2014-5-26", 18], ["2014-6-2", 19], ["2014-6-1", 19], ["2014-5-18", 4], ["2014-5-19", 27]]
In [10]: sorted(l, key=lambda (date, _): map(int, date.split('-')))
Out[10]:
[['2014-5-18', 4],
['2014-5-19', 27],
['2014-5-20', 69],
['2014-5-21', 114],
['2014-5-22', 1225],
['2014-5-23', 16],
['2014-5-24', 19],
['2014-5-25', 18],
['2014-5-26', 18],
['2014-5-27', 10],
['2014-5-28', 16],
['2014-5-29', 19],
['2014-5-30', 20],
['2014-5-31', 17],
['2014-6-1', 19],
['2014-6-2', 19]]
Post a Comment for "Python: Sort List By Date?"