I have been set a task to sort a given list of tuples through a parameter of a function like this. I have to get the function to operate with only two lines of code.
The sort order is as such:
So for example one doctest would be:
sortStudents([('Tim Smith', 54), ('Anna Smith', 88), ('Barry Thomas', 88)])
return (sorted(a, key=lambda x: (-x, x.split(), x.split())))
sortStudents([('Tim Jones', 'C'), ('Anna Smith', 'B'), ('Barry Thomas', 'A')])
You need to check the grade type (string or integer) to determine how the sorting should be done:
def sortStudents(a): return sorted(a, key=lambda x: [-x if isinstance(x, int) else x] + x.split()[::-1])
You may also the name split once, and reverse the list, instead of splitting twice.