Problem Sorting

Hello I get a problem sorting because it dosen´t sort M1, M2, M3… it does M1, M10, M100

1 Like

@bimcar

# 🎯 strings sorted
OUT = sorted(IN[0], key=lambda x: int(x[1:]))
4 Likes

Just to explain what’s going on here, you’re getting “string sorting” since your values are strings (i.e. the “1” in “10” comes before “2”). What you’re after is called “natural sorting”. You can get natural sorting by converting the numeral portion of your string to integers (see @Draxl_Andreas’s solution) or you can use one of the custom nodes that uses natural sorting ( GeniusLoci and Orchid come to mind).

5 Likes

I did it using the GenuisLoci’s node, thanks for the explanation about the natural sorting.

Hi Draxl thanks for the explanation, I found a GeniusLoci’s node that sorted but now I have the question about your comment (OUT = sorted(IN[0], key=lambda x: int(x[1:]))) should I create a node whit this code?

1 Like

@bimcar

yes a python node where you can paste this code

Thank Draxl

1 Like