[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Spacecoder19/pythonprojects/main/sort-a-list-python [Back]  [Original]

# 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')

Web Proxy Viewer  |  New URL  |  Original Page