Numerically order a list based on a pattern
<p>I have a list of file names in the form:</p>

<pre><code>['comm_1_1.txt', 'comm_1_10.txt', 'comm_1_11.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt']
</code></pre>

<p>I wonder how to sort this list numerically to obtain the output:</p>

<pre><code>['comm_1_1.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt', 'comm_1_10.txt', 'comm_1_11.txt']
</code></pre>

<p>You should split needed numbers and convert them to <code>int</code></p>

<pre><code>ss = ['comm_1_1.txt', 'comm_1_10.txt', 'comm_1_11.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt']

def numeric(i):
    return tuple(map(int, i.replace('.txt', '').split('_')[1:]))

sorted(ss, key=numeric)
# ['comm_1_1.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt', 'comm_1_10.txt', 'comm_1_11.txt']
</code></pre>

python sorting