I have the following OrderedDict
:
from collections import OrderedDict
a = OrderedDict()
a['2016:April'] = 1
a['2016:January'] = 2
a['2017:February'] = 3
a['2015:November'] = 4
I would like to sort the dictionary by the keys in chronological order so that the result is:
OrderedDict([('2015:November', 4), ('2016:January', 2), ('2016:April', 1), ('2017:February', 3)])
You need to recreate the
OrderedDict
by passing sorted items; (OrderedDict remembers key insertion order)