I can't seem to be able to get a lambda function to sort a list of tuples by name(after being sorted by grades first). Here's the code:
def sortStudents(a):
sorted(a, key=lambda b: (b[1],str(b[0])))
print(a[::-1])
I'm running a doctest on it and it fails when it has to sort the students by name if they have the same grade. It just returns them without sorting them by name.
Expected:
[('Barry Thomas', 88), ('Tim Smith', 54), ('Yulia Smith', 54)]
Got:
[('Barry Thomas', 88), ('Yulia Smith', 54), ('Tim Smith', 54)]
I've found another post on here trying to do the same, and the answerer suggested the same thing i did, but it's not really working properly. Any help is greatly appreciated!
EDIT:
here are the doctests:
"""
>>> sortStudents([('Tim Jones', 54), ('Anna Smith', 56), ('Barry Thomas', 88)])
[('Barry Thomas', 88), ('Anna Smith', 56), ('Tim Jones', 54)]
>>> sortStudents([('Tim Smith', 54), ('Anna Smith', 88), ('Barry Thomas', 88)])
[('Anna Smith', 88), ('Barry Thomas', 88), ('Tim Smith', 54)]
>>> sortStudents([('Tim Smith', 54), ('Anna Smith', 54), ('Barry Thomas', 88)])
[('Barry Thomas', 88), ('Anna Smith', 54), ('Tim Smith', 54)]
>>> sortStudents([('Tim Smith', 54), ('Yulia Smith', 54), ('Barry Thomas', 88)])
[('Barry Thomas', 88), ('Tim Smith', 54), ('Yulia Smith', 54)]
"""