sorting python list according to given texts
Question: I have the following list:

    
    
    l = ['fig_1.png', 'fig_10.png', 'fig_4.png', 'fig_2.png']
    

Expected after sort:

    
    
    result = ['fig_1.png', 'fig_2.png', 'fig_4.png', 'fig_10.png']
    

I tried as:

result = sorted(l)

But,

`['fig_1.png', 'fig_10.png', 'fig_2.png', 'fig_4.png']`


Answer: You cannot print `l.sort()` as it is in place sorting.

Use

    
    
    l = ['fig_1.png', 'fig_3.png', 'fig_4.png', 'fig_2.png']
    l.sort() #this will print none as it is in place and does not return anything
    print l
    

Output:`['fig_1.png', 'fig_2.png', 'fig_3.png', 'fig_4.png']`

    
    
    import re
    l = ['fig_1.png', 'fig_10.png', 'fig_4.png', 'fig_2.png']
    l.sort(key=lambda x:int(re.findall(r"(?<=_)\d+(?=\.)",x)[0]))
    print l
    

