There is a list of dictionaries

list_dict1 = [{'name': 'Valera', 'age': 29}, {'name': 'Bob', 'age': 24}, {'name': 'Boris', 'age': 19}, {'name': 'Natasha', 'age': 27}, {'name': 'Kate', 'age': 32}] 

The task consists in the following: It is necessary to sort the list by the value of the key “extension”, without using the built-in functions, I tried to do it using the bubble method as follows:

 ages = [(x['age']) for x in list_dict1] for num1 in range(0, len(ages)): for num2 in range(0, len(ages)-num1-1): if int(ages[num2]) > int(ages[num2+1]): list_dict1[num2], list_dict1[num2+1] = list_dict1[num2+1], list_dict1[num2] print(list_dict1) 

the result is not correct:

 [{'age': 24, 'name': 'Bob'}, {'age': 29, 'name': 'Valera'}, {'age': 19, 'name': 'Boris'}, {'age': 27, 'name': 'Natasha'}, {'age': 32, 'name': 'Kate'}] 

Tell me what my mistake is, I will be grateful for any help. Thank you. Sincerely, V.S.V.

    1 answer 1

     n = len(list_dict1) for i in range(n): for j in range(ni-1): if int(list_dict1[j]['age']) > int(list_dict1[j+1]['age']): list_dict1[j], list_dict1[j+1] = list_dict1[j+1], list_dict1[j] print(list_dict1) 

    but it would be more correct to arrange it as a function