5

I have a dictionary like below:

dt = {'2016-12-15 18:12:17': 1, '2016-12-15 17:40:39': 1, '2016-12-15 15:53:42': 1, '2016-12-15 15:51:41': 1, '2016-12-15 15:49:55': 1, '2016-12-15 15:49:54': 1, '2016-12-15 15:48:05': 1, '2016-12-15 15:27:38': 1, '2016-12-15 09:05:03': 1, '2016-12-15 08:43:45': 1, '2016-12-15 07:29:15': 1, '2016-12-15 05:56:58': 1, '2016-12-14 14:33:31': 1, '2016-12-14 14:33:30': 1, '2016-12-14 14:33:29': 1, '2016-12-14 14:33:28': 1, '2016-12-14 13:24:55': 1, '2016-12-14 13:15:51': 1, '2016-12-14 13:05:38': 1, '2016-12-14 12:58:47': 1}

I want to sort the dictionary on datetime key.

I tried :

dt = ordereddict.OrderedDict()

But it is not working please help.

4
  • an ordered dict maintains the insertion order it doesn't sort the keys by value, besides you have strings not datetimes. You'd have to sort the keys yourself and make a new ordered dict Commented Dec 16, 2016 at 10:14
  • 1
    Well, yes, ordereddict.OrderedDict() by itself won't do much. What exactly does "doesn't work" mean? Errors? Is there any more code than this which you tried? Commented Dec 16, 2016 at 10:14
  • Get the keys by dt.keys() and sort them. Then access the values by the keys. Commented Dec 16, 2016 at 10:15
  • 2
    ordered = OrderedDict(sorted(mydict.items(), key=lambda t: t[0])) Commented Dec 16, 2016 at 10:15

2 Answers 2

8

Convert the keys in dt into datetime objects, for instance, using dateutil.parser.parse,

from dateutil.parser import parse
from collections import OrderedDict

new_d = OrderedDict(sorted(dt.items(), key=lambda x: parse(x[0])))

print(new_d)
'''
OrderedDict([   ('2016-12-14 12:58:47', 1), 
                ('2016-12-14 13:05:38', 1), 
                ('2016-12-14 13:15:51', 1), 
                ('2016-12-14 13:24:55', 1), 
                ('2016-12-14 14:33:28', 1), ('2016-12-14 14:33:29', 1), ('2016-12-14 14:33:30', 1), ('2016-12-14 14:33:31', 1), ('2016-12-15 05:56:58', 1), ('2016-12-15 07:29:15', 1), ('2016-12-15 08:43:45', 1), ('2016-12-15 09:05:03', 1), ('2016-12-15 15:27:38', 1), ('2016-12-15 15:48:05', 1), ('2016-12-15 15:49:54', 1), ('2016-12-15 15:49:55', 1), ('2016-12-15 15:51:41', 1), ('2016-12-15 15:53:42', 1), ('2016-12-15 17:40:39', 1), ('2016-12-15 18:12:17', 1)])

'''

In your specific case, as pointed by @AndreaCorbellini, sorting strings directly works fine, i.e.

new_d = OrderedDict(sorted(dt.items()))
Sign up to request clarification or add additional context in comments.

2 Comments

+1, just a remark: there's no need to conver to datetime: sorting strings written in that format will work just fine
@AndreaCorbellini, thx for pointing out this, I just added it to my answer.
0

This works and doesn't need dateutil:

import datetime
sorted(dt.items(), key=lambda x: datetime.datetime.fromisoformat(x[0]))
[('2016-12-15 18:12:17', 1), ('2016-12-15 17:40:39', 1), ('2016-12-15 15:53:42', 1), ('2016-12-15 15:51:41', 1), ('2016-12-15 15:49:55', 1), ('2016-12-15 15:49:54', 1), ('2016-12-15 15:48:05', 1), ('2016-12-15 15:27:38', 1), ('2016-12-15 09:05:03', 1), ('2016-12-15 08:43:45', 1), ('2016-12-15 07:29:15', 1), ('2016-12-15 05:56:58', 1), ('2016-12-14 14:33:31', 1), ('2016-12-14 14:33:30', 1), ...]

If you want the oldest date first, just provide reverse=True to sorted

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.