Search code examples
pythonsortingfile-sorting

Sorting list of file name with text, number and underscore in Python


I have a list of filenames, some of them have only text, some of them have text and number, and some of them have all.

Example:

[ 'mango_1.jpg', 'dog005.jpg', 'guru_2018_01_01.png', 'dog008.jpg', 'mango_6.jpg', 'guru_2018_5_23.png', 'dog01.png', 'mango_11.jpg', 'mango2.jpg', 'guru_2018_02_5.png', 'guru_2019_08_23.jpg', 'dog9.jpg', 'mango05.jpg' ]

My Code is :

import re
## ref: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
def sort_nicely( l ):
    """ Sort the given list in the way that humans expect.
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    l.sort( key=alphanum_key )
    return print(l)

Actual output:

['dog01.png', 'dog005.jpg', 'dog008.jpg', 'dog9.jpg', 'guru_2018_01_01.png', 'guru_2018_02_5.png', 'guru_2018_5_23.png', 'guru_2019_08_23.jpg', 'mango2.jpg', 'mango05.jpg', 'mango_1.jpg', 'mango_6.jpg', 'mango_11.jpg']

Expected output:

['dog01.png', 'dog005.jpg', 'dog008.jpg', 'dog9.jpg', 'guru_2018_01_01.png', 'guru_2018_02_5.png', 'guru_2018_5_23.png', 'guru_2019_08_23.jpg', 'mango_1.jpg', 'mango2.jpg', 'mango5.jpg', 'mango_6.jpg', 'mango_11.jpg']

How do I get the expected output?


Solution

  • Looks like you are not giving any significance to _ character in that case, modify your code to exclude that

    import re
    ## ref: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
    def sort_nicely( l ):
        """ Sort the given list in the way that humans expect.
        """
        convert = lambda text: int(text) if text.isdigit() else text
        alphanum_key = lambda key: [ convert(c.replace("_","")) for c in re.split('([0-9]+)', key) ]
        l.sort( key=alphanum_key )
        return print(l)
        ```