mydict = {'b':10, 'c':10, 'a':10, 'd':20}
字典根据key排序
>>> sorted(mydict.iteritems(), key=lambda x:x[0])
[('a', 10), ('b', 10), ('c', 10), ('d', 20)]
字典根据value排序
>>> sorted(mydict.iteritems(), key=lambda x:x[1])
[('a', 10), ('c', 10), ('b', 10), ('d', 20)]
字典先按key排序,然后按value排序
>>> sorted(mydict.iteritems(), key=lambda x:(x[0],x[1]))
[('a', 10), ('b', 10), ('c', 10), ('d', 20)]
字典先按value排序,然后按key排序
>>> sorted(mydict.iteritems(), key=lambda x:(x[1],x[0]))
[('a', 10), ('b', 10), ('c', 10), ('d', 20)]
字典先按value升序排序,然后按key降序排序
>>> sorted(mydict.iteritems(), key=lambda x:(x[1],-ord(x[0])))
[('c', 10), ('b', 10), ('a', 10), ('d', 20)]
字典先按value降序排序,然后按key升序排序
>>> sorted(mydict.iteritems(), key=lambda x:(x[1],-ord(x[0])), reverse=True)
[('d', 20), ('a', 10), ('b', 10), ('c', 10)]