Sort a number list which contains text

Hi all,

I’m trying to sort a list of numbers with letters, but no luck so far.

Any help is much appreciated. Thank you

Rule number 1 when working with names that contain numbers : zero-stuffing !

I managed to add 0 between the first one or two characters and the integers, so the sort text works properly. (11.0 KB)


I have here a Python script component that uses a regular expression to help with the sorting.

Essentially using the form of Name + number + extra string. Then let the sort happen on those as it should.

The reason that your attempt fails is that everything is handled as one string.

The code in the component is:

import re

pattern = re.compile("(?P<first>\D*)(?P<number>\d*)(?P<second>\D*)")

vals = [pattern.match(v).groups() for v in Values]
SortedValues = [v[0] + v[1] + v[2] for v in vals] (10.3 KB)


Thanks magicteddy.
I like your approach.

Thanks Nathan.
This one is good, but it places TC10 & TC100… in front of TC2, unfortunately.

Nice solution @magicteddy … i think I never used Sort Text component, you used it wisely!
I worked on your definition… (10.6 KB)


Right then first sort on the number, then on the rest I guess. Shouldn’t be too hard to switch things around in the Python component.

1 Like

Hi @hldk,

A bit late perhaps, but for interest here’s a short python script that will correctly sort your list. (10.4 KB)

It combines concepts from Nathan’s and magicteddy’s solutions: regex to split the strings into alpha and numeric parts, and padding the numeric parts to a common length so they can be sorted as strings.



Oh, and for a much more comprehensive embedded numerics sorting solution for Python, you can import the natsort module from PyPI. This parses your list values and recognises various number formats so you don’t need to split them out yourself.

Note that for Python 2.7 you need an old version: pip install natsort==3.3.0. The current version (8.2.0) only works with Python 3.