I have a following dictionary :
{2009: [12, 11, 10, 9], 2010: [1]}
I’m trying to reverse-sort it, so that 2010 comes first. Here’s the code :
def dictSort(dict):
items = dict.items()
items.sort(reverse=True)
dict = {}
for item in items:
dict[item[0]] = item[1]
return dict
But in return I get the same dictionary. Until the for loop everything looks fine. Why is that ?
Dictionary keys are not ordered. Think of a dict as a set of key/value pairs.
This is coming in Python 3.1:
http://docs.python.org/dev/library/collections.html#collections.OrderedDict