sorted(iterable, cmp=None, key=None, reverse=False)
key -- 主要是用来进行比较的元素,只有一个参数
>>> L=[('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, key=lambda x:x[1]) # 利用key (x表示每个元素,如('b',2))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=lambda s: s[2]) # 按年龄排序, 利用key (s表示每个元素,如('john', 'A', 15))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
https://www.runoob.com/python/python-func-sorted.html
按键(key)或值(value)对字典进行排序
按值(value)排序
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323
sorted(key_value.items(), key = lambda kv:(kv[1], kv[0])) # kv表示(2,56),如果不加.items()则默认只返回key
按值(value)排序:
[(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)]
https://www.runoob.com/python3/python-sort-dictionaries-by-key-or-value.html
list.sort(cmp=None, key=None, reverse=False)
https://www.runoob.com/python/att-list-sort.html



