I am trying to sort a list of tuples. They're in this format:
("First Last", 3, 0)
Or in other words:
(string, int, int)
I want to sort by the string value (first tuple element). I found how to sort a list of tuples by a certain element from this awesome answer: https://stackoverflow.com/a/3121985/8887398
This was my code:
# Yes, I do want to start from element 1 btw
myList[1:].sort(key=lambda tup: tup[0])
This worked great when I had only first names as the values of the strings in my tuples, such as:
("George", 8, 3)
Then I added last names, such as:
("George Manning", 8, 3)
It no longer sorted correctly, so I tried this:
myList[1:].sort(key=lambda tup: (tup[0].split(" ")[1]))
I was so confident this would work. It doesn't. I'm confused as I know my split
method is correctly pulling the last name from debugging. What am I doing wrong? How can I sort my list by last name?
Here's an example. Yes they're fake names:
myList = [
("NAME", "SOME LABEL 1", "SOME LABEL 2"),
("Kevin Lee", 45, 4),
("John Bowes", 35, 2),
("George Smith", 8, 3),
("Gina Marnico", 40, 3),
("Alice Gordon", 48, 7),
("Lee Jackson", 49, 7),
("Adam Hao", 50, 4),
("Adrian Benco", 23, 2),
("Jessica Farner", 43, 20),
("Greg Hyde", 34, 20),
("Ryan Valins", 39, 7),
("Gary Funa", 49, 7),
("Sam Tuno", 15, 4),
("Katy Sendej", 30, 2),
("Jessica Randolf", 44, 8),
("Gina Gundo", 47, 30)
]
myList[1:].sort(key=lambda tup: (tup[0].split(" ")[1]))
I skip the first value because it's labeling information. I want that element to stay the same, and the rest of the list to be sorted by last name.