How can I sort by name and age in PYTHON? I have the following list in .txt file:
John, 14
Mike, 18
Marco, 25
Michael, 33
I want to sort this by name and by age. I wrote this code but it doesn't work:
file = open("people.txt", "r")
data = file.readlines()
i = 0
for line in data:
name, age = line.split(',')
list = [name, age]
i += 1
print("For sorting by name press (1);")
print("For sorting by age press (2);")
z = eval(input())
if z == 1:
list.sort(key=lambda x: x.name, reverse=True)
print([item.name for item in list])
Thank you very much guys :)