Convert names to tuples of integers and compare tuples:
def splittedname(s): return tuple(int(x) for x in s.split('.')) splittedname(s1) > splittedname(s2)
Refresh . Since your names can apparently contain characters other than numbers, you need to check the ValueError and leave any values that cannot be converted to int without changes:
import re def tryint(x): try: return int(x) except ValueError: return x def splittedname(s): return tuple(tryint(x) for x in re.split('([0-9]+)', s))
To sort the list of names, use splittedname as the key function for sorted :
>>> names = ['YT4.11', '4.3', 'YT4.2', '4.10', 'PT2.19', 'PT2.9'] >>> sorted(names, key=splittedname) ['4.3', '4.10', 'PT2.9', 'PT2.19', 'YT4.2', 'YT4.11']
source share