If I have a list and I want to keep adding lines to it and sorting them alphabetically by their last name, how could this be done? Sorted only seems to rearrange them by the first letter of the string.
line = "James Edward" #Example line
linesList.append("".join(line)) #Add it to a list
linesList = sorted(linesList) #Sort alphabetically
linesList.sort(key=lambda s: s.split()[1])
More info: https://wiki.python.org/moin/HowTo/Sorting#Key_Functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With