Sort elements in a list

Hi there,

How can I sort assemblies in the same order as they are listed in the project browser.

Notice 11A and 11B. Please suggest if there’s a shorter way too.

import re

liste = IN[0]

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(liste)


IF you are looking to set the numbering part to three characters as standard for all the assembly names, please check this.

Assemblies.dyn (21.2 KB)

1 Like

List.Sort after List.GetItematIndex maybe… because he wants to sort them naturally.

1 Like

I don’t know if sorting them is necessary since you’re working off of the original elements, the list values will still be aligned. Despite the fact that they are not sorted, when you go to rename the assemblies, they’ll still rename the correct assembly that the new data was created from. If you were creating the names from scratch, then you would want to get the list values aligned. Since that’s not what it looks like to me, I wouldn’t really bother with sorting them.

Take this example of the wall types I have in the sample arch template, the walls in the project browser are sorted alphanumerically. However, when I pull them in Dynamo, they are not sorted as it is in the project browser. Regardless if it’s sorted, I am working off of that original list of elements and modifying the name in the same order as it was grabbed in Dynamo and not changing the order. When I go to set the name of the wall to the new name it’ll set correctly because of the elements and the new name lists match.

Hope this helps and makes sense!


You’re right. But in my case I need to accommodate the Assembly 11A and Assembly 11B as Assembly 11 and Assembly 12 respectively and then rename all the following assemblies by ordering one number higher (13, 14…).

a = List.Clean(String.ToNumber(String.Split(str, "")),false);
b = List.SortByKey(str, String.ToNumber(String.Concat(a + "")))["sorted list"];

It’s working but I don’t know why 11B is coming before 11A.

I have 2 questions about this method.

  1. I have to put a lot of “DSCore” before the actual OOTB node notations. Am I at risk while using this in another system where I may not have custom packages loaded (different definitions)?
  2. If I unravel this code block into separate nodes, I get an innocuous warning at String.ToNumber node which might put off another user of this Dynamo graph?


This works fine. But I’m wondering how can I now sort the actual elements using this sorted names list.

You were in the right way: instead of using Sort node, use List.SortByKey so elements (assemblies) and their names will ordered and keeper accordingly :wink:

Okay. I got it working for me. Just used List.AllIdicesOf to get the indexes to sort the elements.
Thanks @Deniz_Maral

1 Like