-1

How do you sort in non-increasing order a list of team names and scores, and secondly in lexicographical order? I have the following line of Python that I am trying to understand.

sigList.sort(key = lambda x : str(len(x)-1) + x[0], reverse= True)

From my understanding, this sorts in non-increasing order, but not in lexicographical order. I have a team name, and then the score for that team. I want it sorted by the highest score first, then by the team name. How do I do both?

Example:

BOB TEAM 9
DALE TEAM 7
KIM TEAM 3

The sort function in Python should automatically sort in lexicographical order, but I am only able to get my output sorted in non-increasing order.

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

Convert the string to a tuple with the fields you want to sort by in that order.

To get the highest score first, convert it to a negative number.

sigList = ['BOB TEAM 9', 'KIM TEAM 3', 'DALE TEAM 7', 'FRED TEAM 3']
sigList.sort(key = lambda x: (-int(x.split()[-1]), x.split()[:-1]))
print(sigList) # ['BOB TEAM 9', 'DALE TEAM 7', 'FRED TEAM 3', 'KIM TEAM 3']
Barmar
  • 741,623
  • 53
  • 500
  • 612