sorting list of a sentence and number
<p>I have checked several of the answers on how to sort lists in python, but I can't figure this one out.</p>

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

<p>Is there a way to sort by the number after each sentence? </p>

<p>I am trying to learn this stuff on my own, so I tried stuff like:</p>

<pre><code>def sortMyList(string):
    return len(string)-1

sortedList = sorted(MyList, key=sortMyList())
</code></pre>

<p>But of course it doesn't work becaue sortMyList expects one parameter.</p>

<p>You can do this</p>

<pre><code>&gt;&gt;&gt; sorted(l, key=lambda x : int(x.split(',')[-1]))
['Today is a good day,1', 'yesterday was a strange day,2', 'feeling hopeful,3']
&gt;&gt;&gt; 
</code></pre>

<p>This would also work if you happen to have numbers in your string that have more than one digit</p>

<pre><code>&gt;&gt;&gt; l = ['Today is a good day,12', 'yesterday was a strange day,21', 'feeling hopeful,23']
&gt;&gt;&gt; sorted(l, key=lambda x : int(x.split(',')[1]))
['Today is a good day,12', 'yesterday was a strange day,21', 'feeling hopeful,23'] # still works
&gt;&gt;&gt; sorted(l, key=lambda x : x[-1])
['yesterday was a strange day,21', 'Today is a good day,12', 'feeling hopeful,23'] # doesn't work in this scenario
</code></pre>

python python-3.x