列表使用 sort().
执行选择排序条件的函数是另一个选项。
def myFunc(e):
return e['year']
cars = [
{'car': 'Peugeot', 'year': 2015},
{'car': '三菱', '年份': 2010},
{'car': '宝马', '年份': 2022},
{'car': '雪铁龙', '年份': 2012}
]
cars.sort(key=myFunc)
[{'car': '三菱', '年份': 2010}, {'car': '雪铁龙', '年份': 2012}, {'car': '标致', '年份': 2015}, {'car': '宝马', '年份': 2022}]
Please disable your ad blocker and refresh the window to use this website.