这里给出一个示例,假设有一个包含学生姓名、课程和成绩的列表,我们可以使用 Python 的内置排序函数 `sorted()` 来按照不同的条件进行排序。
```python
students = [
{"name": "Alice", "course": "Math", "score": 87},
{"name": "Bob", "course": "English", "score": 92},
{"name": "Charlie", "course": "History", "score": 78},
{"name": "David", "course": "Math", "score": 90},
{"name": "Eva", "course": "English", "score": 85},
{"name": "Frank", "course": "History", "score": 81},
]
# 按照课程升序排列
sorted_students = sorted(students, key=lambda x: x["course"])
print(sorted_students)
# 按照成绩降序排列
sorted_students = sorted(students, key=lambda x: x["score"], reverse=True)
print(sorted_students)
# 按照课程和成绩升序排列
sorted_students = sorted(students, key=lambda x: (x["course"], x["score"]))
print(sorted_students)
```
输出结果分别为:
```
[{'name': 'Alice', 'course': 'Math', 'score': 87}, {'name': 'Bob', 'course': 'English', 'score': 92}, {'name': 'Eva', 'course': 'English', 'score': 85}, {'name': 'Charlie', 'course': 'History', 'score': 78}, {'name': 'Frank', 'course': 'History', 'score': 81}, {'name': 'David', 'course': 'Math', 'score': 90}]
[{'name': 'Bob', 'course': 'English', 'score': 92}, {'name': 'David', 'course': 'Math', 'score': 90}, {'name': 'Alice', 'course': 'Math', 'score': 87}, {'name': 'Eva', 'course': 'English', 'score': 85}, {'name': 'Frank', 'course': 'History', 'score': 81}, {'name': 'Charlie', 'course': 'History', 'score': 78}]
[{'name': 'Bob', 'course': 'English', 'score': 92}, {'name': 'Eva', 'course': 'English', 'score': 85}, {'name': 'Alice', 'course': 'Math', 'score': 87}, {'name': 'David', 'course': 'Math', 'score': 90}, {'name': 'Charlie', 'course': 'History', 'score': 78}, {'name': 'Frank', 'course': 'History', 'score': 81}]
```
以上代码中,`key` 参数指定了排序的依据,可以使用 lambda 函数来指定排序的规则。在第一个例子中,我们按照课程名称进行升序排列;在第二个例子中,我们按照成绩进行降序排列;在第三个例子中,我们先按照课程名称进行升序排列,如果课程名称相同,则按照成绩进行升序排列。