komodo komodo - 2 months ago 5
Python Question

sorting list of a sentence and number

I have checked several of the answers on how to sort lists in python, but I can't figure this one out.

Let's say I have a list like this:
['Today is a good day,1', 'yesterday was a strange day,2', 'feeling hopeful,3']

Is there a way to sort by the number after each sentence?

I am trying to learn this stuff on my own, so I tried stuff like:

def sortMyList(string):
return len(string)-1

sortedList = sorted(MyList, key=sortMyList())


But of course it doesn't work becaue sortMyList expects one parameter.

Answer

This worked for me:

sorted(myList, key=lambda x: x[-1])

If you need to go into double digits:

sorted(myList, key=lambda x: int(x.split(',')[1]))