I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where my keys are dates. However it does not order them correctly, I imagine it may be ordering based on the ID.
Does anyone have any suggestions of how this could be done?
OrderedDict, according to its docstring, is a kind of dict that remembers insertion order. Thus, you need to manually insert the key/value pairs in the correct order.
edit: See utdemir's answer for a better example. Using
operator.itemgetter
gives you better performance (60% faster, I use the benchmark code below) and it's a better coding style. And you can applyOrderedDict
directly tosorted(...)
.