35

How to sort the below array of dates on python 2.4

 timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30', '2010-11-26', '2010-11-23', '2010-11-22', '2010-11-16']
1
  • 13
    Why are you using Python 2.4? Commented Mar 2, 2011 at 11:34

9 Answers 9

74
>>> import datetime
>>> dates = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in timestamps]
>>> dates.sort()
>>> sorteddates = [datetime.datetime.strftime(ts, "%Y-%m-%d") for ts in dates]
>>> sorteddates
['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-11-16', '2010-11-
22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13', '2011-02-04', '2011
-06-02', '2011-08-05', '2011-11-30']
Sign up to request clarification or add additional context in comments.

4 Comments

This is the most pythonic way I think. It scales to different formats of time, unlike the accepted answer.
best answer (+1) but you probably should not call the last object sorted as it is also the name of a function
How to disaccept another answer and accept this one? :)
this is most logical approach of sorting dates, if they are in string format.
37
sorted(timestamps, key=lambda d: map(int, d.split('-')))

3 Comments

Cool. Raises a TypeError in Python 3, though.
I don't have Python 3 installed, but would that be because map returns an iterable? In that case, the key function should be lambda d: tuple(map(int, d.split('-'))).
If you need the latest date in the array, you can add the reverse=True argument.
13

Just doing that:

timestamps.sort()

result:

['2010-1-12',
 '2010-1-14',
 '2010-11-16',
 '2010-11-22',
 '2010-11-23',
 '2010-11-26',
 '2010-12-02',
 '2010-12-13',
 '2010-2-07',
 '2010-2-11',
 '2011-02-04',
 '2011-06-2',
 '2011-08-05',
 '2011-11-30']

The order year-month-day allow such a sorting because a day changes before a month and a month changes before a year when time is passing.

It's like for a number: the unity digit (the rightmost digit) changes before the ten digit, and this latter changes before the hundred digit, when 1 is progressively added.

And there is the fact that sort() processes from left to right : if the characters at one precise position are the same in two strings to sort, it will examine the two characters in the two string at the following position to decide which one is logically preceding.

Plus the fact that '0' < '1' is True, '1' < '2' is True etc

3 Comments

That way, 2010-10-1 will be sorted before 2010-2-1.
@Tim Pietzcker Oh yes. It only works if the numbers are zfilled(2) which is more frequently the case
and your result is wrong, because '2010-2-07' comes after '2010-12-13' , this is radix sort
7
>>> import time
>>> timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30', '2010-11-26', '2010-11-23', '2010-11-22', '2010-11-16']
>>> timestamps.sort(key=lambda x: time.mktime(time.strptime(x,"%Y-%m-%d")))
>>> timestamps
['2010-1-12', '2010-1-14', '2010-2-07', '2010-2-11', '2010-11-16', '2010-11-22', '2010-11-23', '2010-11-26', '2010-12-02', '2010-12-13', '2011-02-04', '2011-06-2', '2011-08-05', '2011-11-30']

Comments

2

If you sort them into the same format you can just call timestamps.sort()

Comments

1
map(lambda x:x[1], sorted(map(lambda a:[map(int,a.split('-')),a], timestamps)))

['2010-1-12',
 '2010-1-14',
 '2010-2-07',
 '2010-2-11',
 '2010-11-16',
 '2010-11-22',
 '2010-11-23',
 '2010-11-26',
 '2010-12-02',
 '2010-12-13',
 '2011-02-04',
 '2011-06-2',
 '2011-08-05',
 '2011-11-30']

Comments

1

This sort ascending dates in dd/mm/yyyy format

from datetime import datetime 
c_array=['07/12/2017', '30/01/2018', '31/05/2016', '30/09/2016', '30/01/2017', '31/05/2017']

sorted(c_array, key=lambda x: datetime.strptime(x, "%d/%m/%Y").strftime("%Y-%m-%d"))
#out: ['31/05/2016', '30/09/2016', '30/01/2017', '31/05/2017', '07/12/2017', '30/01/2018']

Comments

1
print(sorted(list_of_strings,key=lambda x :(int(x.split('-')[0]),int(x.split('-')[1]),int(x.split('-')[2])))

Comments

1

In Python 3 and using (my personal favorite) comprehensions. For dates, I would always use Python's datetime lib:

from datetime import date
timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2-07', '2010-12-02', '2011-11-30', '2010-11-26', '2010-11-23', '2010-11-22', '2010-11-14']
timestamps = [date(*[int(y) for y in x.split("-")]) for x in timestamps]
sorted_timestamps = sorted(timestamps)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.