I want to sort grades alphabetically in a function that are in tuples, such as:
sortGrades([('John Smith', 'C'), ('Adam Thomas', 'B'), ('Katie Johnson', 'A')])
and also, numerically, like this:
sortGrades([('John Smith', 54), ('Adam Smith', 54), ('Thomas King', 88)])
So it will sort the tuples by highest score and highest grade. The names should also be sorted alphabetically if there are two people with the same score.
So far my function looks like this:
def sortGrades(list):
return sorted(list, key=lambda x:(-x[1],x[0]))
However this does not seem to work when I sort grades (A, B, C), it returns a "TypeError: bad operand type for unary -: 'str'"
Any idea how to make it work for strings and integers at the same time?
- edited question, need it to work for both strings and ints!
sortGrades()operate without any indication as to which type of list to expect?