Sort Numbers

Hello,

I would like to sort numbers in an order like this:

1.2.8
1.2.9
1.2.10
1.10.12

But with dynamo i get the result:

1.10.12
1.2.10
1.2.8
1.2.9

I will now use the workaround that i use the style like 01.02.03, that works.
Just wanted to ask if there would be any way to sort the list like i wanted to do in first place.

Thanks for any help!

Kind Regards!

@gerhard.p You need something called natural sort.

Try this:

import re

def natural_sort(l): 
    convert = lambda text: int(text) if text.isdigit() else text.lower() 
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] 
    return sorted(l, key = alphanum_key)
    
OUT = natural_sort(IN[0])

Alternatively, you can use the Natural Sort node from Orchid package

4 Likes

Natural sort, thats a thing i know now, thank you so much :grinning:

1 Like