-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-a-list-python
More file actions
19 lines (16 loc) · 590 Bytes
/
sort-a-list-python
File metadata and controls
19 lines (16 loc) · 590 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# sorting using custom key
employees = [
{'Name': 'Piyush', 'age': 25, 'salary': 10000},
{'Name': 'Shweta', 'age': 30, 'salary': 8000},
{'Name': 'John Cena', 'age': 18, 'salary': 1000},
{'Name': 'Masoom', 'age': 40, 'salary': 15000},
]
# sort by name (Ascending order)
employees.sort(key=lambda x: x.get('Name'))
print(employees, end='\n\n')
# sort by Age (Ascending order)
employees.sort(key=lambda x: x.get('age'))
print(employees, end='\n\n')
# sort by salary (Descending order)
employees.sort(key=lambda x: x.get('salary'), reverse=True)
print(employees, end='\n\n')