Created
November 4, 2015 18:24
-
-
Save JenkinsDev/4cb2d00899a90c18d9d4 to your computer and use it in GitHub Desktop.
Sort a Python 3 Dictionary By Date, On Keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
from datetime import datetime | |
def sort_dict_by_date(the_dict, date_format): | |
# Python dicts do not hold their ordering so we need to make it an | |
# ordered dict, after sorting. | |
return OrderedDict(reversed(sorted( | |
the_dict.items(), | |
key=lambda x: datetime.strptime(x[0], date_format) | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment