python order list with lambda

Solutions on MaxInterview for python order list with lambda by the best coders in the world

showing results for - "python order list with lambda"
Ana
23 Apr 2016
1# sorting using custom key
2employees = [
3    {'Name': 'Alan Turing', 'age': 25, 'salary': 10000},
4    {'Name': 'Sharon Lin', 'age': 30, 'salary': 8000},
5    {'Name': 'John Hopkins', 'age': 18, 'salary': 1000},
6    {'Name': 'Mikhail Tal', 'age': 40, 'salary': 15000},
7]
8
9# sort by name (Ascending order)
10employees.sort(key=lambda x: x.get('Name'))
11print(employees, end='\n\n')
12
13# sort by Age (Ascending order)
14employees.sort(key=lambda x: x.get('age'))
15print(employees, end='\n\n')
16
17# sort by salary (Descending order)
18employees.sort(key=lambda x: x.get('salary'), reverse=True)
19print(employees, end='\n\n')