2

I have 3 arrays that are 2d and inside them is a string that is a date i would like to sort these by that date in them. The arrays are all structured like this:

array1 = [[1,'29-04-2013','U11'],[2,'20-05-2013','U11']]
array2 = [[1,'06-05-2013','U13'],[2,'03-06-2013','U13']]
array3 = [[1,'06-03-2013','U15'],[2,'03-07-2013','U15']]

I would like to get them into an array like this:

all = [[1,'06-03-2013','U15'],[1,'29-04-2013','U11'],[1,'06-05-2013','U13'],[2,'20-05-2013','U11'],[2,'03-06-2013','U13'],[2,'03-07-2013','U15']]

I just need some sort of way to approach this as i havent got a clue how i would do it.Thanks for the help in advance

1 Answer 1

7
big_array = array1 + array2 + array3
import dateutil.parser as p
print sorted(big_array,key=lambda x: p.parse(x[1]))

if for somereason you are opposed to dateutil.parser

import datetime
print sorted(big_array,key=lambda x:datetime.datetime.strptime(x[1],"%d-%m-%Y")

the reason I reccommend datetime over the regular time module is that datetime can see as far in the future as Ive tested ... while the time module only works up to like 2035

however you can also do it with the time module

import time
print sorted(big_array,key=lambda x:time.strptime(x[1],"%d-%m-%Y")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that worked i used the datetime one as ive been using that import else where in my program

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.