0

Please let me know how to sort the list alphabetically, but if list contains space or special characters, then it will be sorted "wrong"

My list:

list = ['0001.TIF', '0002.TIF', '0003 (2).TIF', '0003.TIF', '0004 (2).TIF', '0004.TIF', '0005.TIF', '0006.TIF', '0007 (2).TIF', '0007.TIF']

Basic sorting:

for i in sorted(list):
    print (i)

Result is:

0001.TIF
0002.TIF
0003 (2).TIF
0003.TIF
0004 (2).TIF
0004.TIF
0005.TIF
0006.TIF
0007 (2).TIF
0007.TIF

But how do I have to sort list to get result below?

0001.TIF
0002.TIF
0003.TIF
0003 (2).TIF
0004.TIF
0004 (2).TIF
0005.TIF
0006.TIF
0007.TIF
0007 (2).TIF
Tinke
  • 45
  • 3

2 Answers2

2

You want to sort them based on the parts of the filename, so split that out and sort by that. See Split filenames with python

data = ['0001.TIF', '0002.TIF', '0003 (2).TIF', '0003.TIF', '0004 (2).TIF', '0004.TIF', '0005.TIF', '0006.TIF', '0007 (2).TIF', '0007.TIF']
desired = sorted(data, key=lambda f: os.path.splitext(os.path.basename(f)))
for filename in desired:
    print(filename)

I included the extension as a tie breaker, although you might want to change this and sort them by extension first, then the filename part.

Also don't use "list" as a variable name. That's a class name, and a pretty important one.

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
2

Another solution: Create key function where you can use re to extract all digit-groups and convert them to integers:

import re

lst = [
    "0001.TIF",
    "0002.TIF",
    "0003 (2).TIF",
    "0003.TIF",
    "0004 (2).TIF",
    "0004.TIF",
    "0005.TIF",
    "0006.TIF",
    "0007 (2).TIF",
    "0007.TIF",
]


def key_fn(x):
    return tuple(map(int, re.findall(r"\d+", x)))


print(*sorted(lst, key=key_fn), sep="\n")

Prints:

0001.TIF
0002.TIF
0003.TIF
0003 (2).TIF
0004.TIF
0004 (2).TIF
0005.TIF
0006.TIF
0007.TIF
0007 (2).TIF
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    Good point, they may want to just rely on their domain knowledge, and split out the fields in this specific filename format. 4 digits, then an optional copy number. The common element is write your own key function that gives the order you want. – Kenny Ostrom Aug 10 '21 at 15:42