Skip to content

Instantly share code, notes, and snippets.

@JenkinsDev
Created November 4, 2015 18:24
Show Gist options
  • Save JenkinsDev/4cb2d00899a90c18d9d4 to your computer and use it in GitHub Desktop.
Save JenkinsDev/4cb2d00899a90c18d9d4 to your computer and use it in GitHub Desktop.
Sort a Python 3 Dictionary By Date, On Keys
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