# 定义学生信息列表
复制
students = [ {"name": "jack", "score": 70}, {"name": "peter", "score": 96}, {"name": "Tom", "score": 70}, {"name": "smith", "score": 67} ]
从高到低排序
sorted_students_high_to_low = sorted(students, key=lambda x: (-x["score"], students.index(x)))
从低到高排序
sorted_students_low_to_high = sorted(students, key=lambda x: (x["score"], students.index(x)))
打印排序结果
print("从高到低:") for student in sorted_students_high_to_low: print(student["name"], student["score"])
print("\n从低到高:") for student in sorted_students_low_to_high: print(student["name"], student["score"])