There\'s an existing function that ends in the following, where d is a dictionary:
return d.iteritems()
that returns an unsort
In general, one may sort a dict like so:
for k in sorted(d):
print k, d[k]
For the specific case in the question, having a "drop in replacement" for d.iteritems(), add a function like:
def sortdict(d, **opts):
# **opts so any currently supported sorted() options can be passed
for k in sorted(d, **opts):
yield k, d[k]
and so the ending line changes from
return dict.iteritems()
to
return sortdict(dict)
or
return sortdict(dict, reverse = True)