1

I want to sort the following data by date and time.

{'TID': 'B1', 'sno': '2', 'datetime': '04-13-12 17:21:18:83'}
{'TID': 'A1', 'sno': '4', 'datetime': '11-17-12 22:50:59:30'}
{'TID': 'A2', 'sno': '3', 'datetime': '11-17-12 22:51:00:66'}
{'TID': 'B6', 'sno': '2', 'datetime': '10-28-12 07:26:02:19'}
{'TID': 'B0', 'sno': '8', 'datetime': '02-26-16 08:13:01:30'}
{'TID': 'B4', 'sno': '8', 'datetime': '02-26-16 08:13:02:43'}
{'TID': 'C1', 'sno': '9', 'datetime': '03-08-15 17:12:55:81'}
{'TID': 'A2', 'sno': '23', 'datetime': '03-08-15 17:31:31:12'}

Solution proposed here and here don't work for me.

Hee is my code:

mylist=[]
with open(path+outfile) as fid:
    for line in fid:
        mylist.append(eval(line))


from operator import itemgetter
newlist = sorted(mylist, key=itemgetter('datetime')) 
for item in newlist:
    print(str(item))

here is the output of the above code:

 {'sno': '8', 'TID': 'B0', 'datetime': '02-26-16 08:13:01:30'}
{'sno': '8', 'TID': 'B4', 'datetime': '02-26-16 08:13:02:43'}
{'sno': '9', 'TID': 'C1', 'datetime': '03-08-15 17:12:55:81'}
{'sno': '23', 'TID': 'A2', 'datetime': '03-08-15 17:31:31:12'}
{'sno': '2', 'TID': 'B1', 'datetime': '04-13-12 17:21:18:83'}
{'sno': '2', 'TID': 'B6', 'datetime': '10-28-12 07:26:02:19'}
{'sno': '4', 'TID': 'A1', 'datetime': '11-17-12 22:50:59:30'}
{'sno': '3', 'TID': 'A2', 'datetime': '11-17-12 22:51:00:66'}

If you notice, the above output is not sorted by date and time.

6
  • what is 83 in this notation 04-13-12 17:21:18:83 ? Commented Jan 30, 2017 at 8:26
  • 1
    If your datetime values were done properly, that other solution would work. Commented Jan 30, 2017 at 8:27
  • 1
    I don't see how it is not sorted. It is sorted alphabetically. Problem is: you have months first, so you need to parse the date first. Commented Jan 30, 2017 at 8:28
  • @Jean-FrançoisFabre. I'm new to python. Can you help me to do that? Commented Jan 30, 2017 at 8:30
  • @chandresh You'd perhaps better just fix your data. Commented Jan 30, 2017 at 8:33

2 Answers 2

3

alphanumerical sort doesn't work here because the date is month-day-year. For it to work properly it should be year-month-day. I defined an helper function to split the date elements and rebuild a tuple so natural tuple sort matches date sort:

def sortable_date(x):
    d,t = x['datetime'].split()
    dtoks = d.split('-')
    #        year     month    day     time
    return (dtoks[2],dtoks[0],dtoks[1],t)

result = sorted(data,key=sortable_date)

Another possibility would be to sort according to parsed date, like this:

result = sorted(data,key=lambda x : time.strptime(x['datetime'],"%m-%d-%y %H:%M:%S:%f"))

with your data I get:

{'TID': 'B1', 'datetime': '04-13-12 17:21:18:83', 'sno': '2'}
{'TID': 'B6', 'datetime': '10-28-12 07:26:02:19', 'sno': '2'}
{'TID': 'A1', 'datetime': '11-17-12 22:50:59:30', 'sno': '4'}
{'TID': 'A2', 'datetime': '11-17-12 22:51:00:66', 'sno': '3'}
{'TID': 'C1', 'datetime': '03-08-15 17:12:55:81', 'sno': '9'}
{'TID': 'A2', 'datetime': '03-08-15 17:31:31:12', 'sno': '23'}
{'TID': 'B0', 'datetime': '02-26-16 08:13:01:30', 'sno': '8'}
{'TID': 'B4', 'datetime': '02-26-16 08:13:02:43', 'sno': '8'}
Sign up to request clarification or add additional context in comments.

1 Comment

I was relying on dateutil, but it failed me :D
2

You can write your own key function to extract and parse the datetime:

import datetime
from operator import itemgetter

mylist = [{'TID': 'B1', 'sno': '2', 'datetime': '04-13-12 17:21:18:83'},
{'TID': 'A1', 'sno': '4', 'datetime': '11-17-12 22:50:59:30'},
{'TID': 'A2', 'sno': '3', 'datetime': '11-17-12 22:51:00:66'},
{'TID': 'B6', 'sno': '2', 'datetime': '10-28-12 07:26:02:19'},
{'TID': 'B0', 'sno': '8', 'datetime': '02-26-16 08:13:01:30'},
{'TID': 'B4', 'sno': '8', 'datetime': '02-26-16 08:13:02:43'},
{'TID': 'C1', 'sno': '9', 'datetime': '03-08-15 17:12:55:81'},
{'TID': 'A2', 'sno': '23', 'datetime': '03-08-15 17:31:31:12'}]

def key_function(item_dictionary):
    '''Extract datetime string from given dictionary, and return the parsed datetime object'''
    datetime_string = item_dictionary['datetime']
    return datetime.datetime.strptime(datetime_string, '%m-%d-%y %H:%M:%S:%f')

mylist.sort(key=key_function)

result:

[{'TID': 'B1', 'datetime': '04-13-12 17:21:18:83', 'sno': '2'},
 {'TID': 'B6', 'datetime': '10-28-12 07:26:02:19', 'sno': '2'},
 {'TID': 'A1', 'datetime': '11-17-12 22:50:59:30', 'sno': '4'},
 {'TID': 'A2', 'datetime': '11-17-12 22:51:00:66', 'sno': '3'},
 {'TID': 'C1', 'datetime': '03-08-15 17:12:55:81', 'sno': '9'},
 {'TID': 'A2', 'datetime': '03-08-15 17:31:31:12', 'sno': '23'},
 {'TID': 'B0', 'datetime': '02-26-16 08:13:01:30', 'sno': '8'},
 {'TID': 'B4', 'datetime': '02-26-16 08:13:02:43', 'sno': '8'}]

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.