I want to find a way to sort strings that have numbers in them by their numerical size.
I found one way to sort strings that contain only numbers, which works well (Sorting numbers in string format with Python) but not when the string is a mix of words and numbers.
In this example I am creating the list in the order that I want, but the sorted() ruins it.
>>> s = ['A_10x05', 'A_10x50', 'A_10x100']
>>> print(sorted(s))
['A_10x05', 'A_10x100', 'A_10x50']
Expected output
['A_10x05', 'A_10x50', 'A_10x100']
A more complex example would be:
>>> s = ['Asset_Castle_Wall_25x400x100_Bottom_01', 'Asset_Castle_Wall_25x400x50_Top_02',
'Asset_Castle_Wall_25x400x10_Bottom_01', 'Asset_Castle_Wall_25x400x300_Top_01']
>>> print(sorted(s))
['Asset_Castle_Wall_25x400x100_Bottom_01', 'Asset_Castle_Wall_25x400x10_Bottom_01', 'Asset_Castle_Wall_25x400x300_Top_01', 'Asset_Castle_Wall_25x400x50_Top_02']
Expected output:
['Asset_Castle_Wall_25x400x10_Bottom_01', 'Asset_Castle_Wall_25x400x50_Top_02', 'Asset_Castle_Wall_25x400x100_Bottom_01', 'Asset_Castle_Wall_25x400x300_Top_01']
I am thinking I would need to split the string by numbers and sort each part, and I can sort the number parts using the solution above. Then where there are multiple strings that start the same way i.e section[i] = ('A_')
I sort section[i+1] and work my way to the end. This feels very complicated though, so maybe there is a better way.