Halo
发布于 2022-06-14 / 341 阅读 / 0 评论 / 0 点赞

排序

  1. map 排序
map_xxx ={}
map_xxx[1] ='a'
map_xxx[3] = 'c'
map_xxx[2] = 'b'
for k, v in map_xxx.items():
    print(k)
result = sorted(map_xxx.keys())
result = sorted(map_xxx.items(), key=lambda x:x[0])

for item in result:
    print(item)
  1. 二维数组排序
listA = [('a', 1), ('c', 3), ('d', 4), ('b', 2)]
sorted(listA, key=lambda x:x[1])

输出结果为:

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

评论