Sorting a list of numbers in a logical order

Hi there,

I’m looking for some help sorting a list of type marks from 01-200.
Currently, this is the way Dynamo sorts them out for me:

SPF-100 it’s on the list before SPF-11, as Dynamo it’s only reading the first digit of the number, but I want to sort them like you would typically do, as in; SPF-10, SPF-11, SPF-12 etc.
How can I get Dynamo to organize the numbers from small to big?

Sorry if this is too obvious!

Thanks,
Ana

  1. Use a String.Replace node to remove the "SPF-" and replace it with an empty string "".
  2. Use a String.PadLeft to append some zeros on the front of the resulting value until all values are 3 or 4 characters long, giving you numbers like 0001, 0010, 0100.
  3. Use a List.SortByKey to sort the data you want to work with (the list input) by the resulting 4 character strings (the keys input). Watch your list levels and lacing in case there are nested lists you want to work with.

If you have different prefixes in the original strings but always keep a hyphen between the prefix and suffix (ie: "other-999") you can use a String.Split node instead and then get the last item from each sublist instead of using the String.Replace.

@anabc Try using the natural sort as described in this solution:

1 Like

Thanks @AmolShah I tried the python script and that worked really nicely!

1 Like