For the list sorting method with the element as the dictionary, sort the element dictionary list
Existing list such
List_to_be_sorted = [{'lv': 44, 'age': 10 },
{'Lv': 12, 'age': 39 },
{'Lv': 88, 'age': 22 },
{'Lv': 6, 'age': 2 },
{'Lv': 3, 'age': 4},]
Sort the list by the value of the age field.
from operator import itemgetterlist_to_be_sorted = [{'lv': 44, 'age': 10}, {'lv': 12, 'age': 39}, {'lv': 88, 'age': 22}, {'lv': 6, 'age': 2}, {'lv': 3, 'age': 4}, ]new_list = sorted(list_to_be_sorted, key=itemgetter('age'))print(new_list)
If you want to use the product of lv and age field value as the sorting standard, you can refer to the following method:
list_to_be_sorted = [{'lv': 44, 'age': 10}, {'lv': 12, 'age': 39}, {'lv': 88, 'age': 22}, {'lv': 6, 'age': 2}, {'lv': 3, 'age': 4}, ]new_list = sorted(list_to_be_sorted, key=lambda k: k['lv'] * k['age'])print(new_list)