# Natural Sort List of Lists by first Index

Hello everyone!

I have List of Lists which I needed to sort by first Index. I found python script and implemented it as you can see in the attachment:

It worked perfectly, however, because of the nature of my list, I need NATURAL SORT, rather then SORT.

I did some research on the forum and found Orchid’s node “List.SortNatural”. Furthermore, I found Kukul’s script for Python, that seem to do the same trick as “List.SortNatural” node.

Now, since I have little coding knowledge, does anyone know how to merge this two codes to create “Natural Sort List of Lists by first Index”?

Also, any other way is just fine to me.

I am copy-pasting Kukul’s code for easy reference:
import re

#input assigned the IN variable
data = IN[0] #list
boolean = IN[1] #Boolean to reverse

def natural_sort(var):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum = lambda key: [convert(t) for t in re.split(’([0-9]+)’, key)]
return sorted(var, key = alphanum, reverse = boolean)

OUT = natural_sort(data)

Thank you and have a nice day!

``````#input assigned the IN variable
data = IN[0] #list
boolean = IN[1] #Boolean to reverse

def natural_sort(var):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum = lambda key: [convert(t) for t in re.split(’([0-9]+)’, key)]
return sorted(var, key = alphanum, reverse = boolean)

result = []
# Order nested lists
for sublist in data:
natOrd = natural_sort(sublist)
result.append(natOrd)

OUT = sorted(result, key=lambda x: x[0])
``````

Hope Kulkul doesn’t mind me butchering his code, haha.

1 Like

Dear architectcoding,

thank you for your swift response!

One problem still remains though:

Note:
I am using Dynamo as I found it within Revit, which means I only searched for and added some packages, but nothing else. Do I need to install/upgrade Dynamo in some way so as to get this code working?

My bad, didn’t realize that the function was not indented.

The three lines after def natural_sort need to be indented.
That’s all, you don’t need anything more.

Dear architectcoding,

I think I am starting to understand the terminology a bit. After I “indented” the suggested lines I received the following message:

Unexpected token. I inspected the code and concluded to replace it anew with " ’ ".
However, I just made it worse:

Dear architectcoding,

I have created this Mock-up, perhaps it could be of use!

MockIp_NaturalSort - ListOfLists.dyn (15.5 KB)

You forgot the `import re` at the top

EDIT: re needs to be imported

Hi Kenzo,

regex module is loaded by default, so no need to import it. Refer to:

I opened up the script, got the missing module for re, added the import re, and it worked. So I would say you need to add it, especially since it was in the very first version of the code that was posted and that was the only time it was working.

Also, regex module is not loaded by default, the path is just available so you don’t need to use a sys.path.append() to find it. You still must import it when wanting to use its commands.

Cool, you have the prof in there, so yeah, I have change my opinion.
Will give it a go and check what is happening.

EDIT: re needs to be imported. Thanks for the clarification Kenzo

Dear Kenzo,

"I opened up the script, got the missing module for re, added the import re, and it worked. "

Hopefully, one last clarification, where/how do I get missing module, and where do I place it on my computer for it to work?

I have I feeling I am asking question about a well know matter, but so far I only used standard modules, and this my first time working on this level.

In any case, thank you for your help and patience!

You just need to type `import re` at the very top of the script.

Sorry, when I typed “got the missing module for re”, I meant that I received the error saying that re was missing.

1 Like

Dear Kenzo!

Thank you for the clarification and for your help.
When I write “re import”, the mock-up script does indeed work.
However, upon inspection it clearly doesn’t do the trick Orchid’s node Natural.Sort does.
It changes the indices of some elements and sorts them wrongly.

Unfortunately it seems we still don’t have final solution for Natural Sort List of Lists by first Index.

``````# Load the Python Standard and DesignScript Libraries
import sys
import clr
import re
from Autodesk.DesignScript.Geometry import *

#input assigned the IN variable
data = IN[0] #list
boolean = IN[1] #Boolean to reverse
check = [d[0] for d in data]

def natural_sort(var):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum = lambda key: [convert(t) for t in re.split('([0-9]+)', key)]
return sorted(var, key = alphanum, reverse = boolean)

result = []
prime = natural_sort(check)

# Order nested lists
for p in prime:
result.append(data[check.index(p)])

OUT = result
``````

Alternatively, all you had to do to make it check by first index was edit the function’s sort key. See below:

``````# Load the Python Standard and DesignScript Libraries
import sys
import clr
import re
from Autodesk.DesignScript.Geometry import *

#input assigned the IN variable
data = IN[0] #list
boolean = IN[1] #Boolean to reverse

def natural_sort(var):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum = lambda key: [convert(t) for t in re.split('([0-9]+)', key[0])]
return sorted(var, key = alphanum, reverse = boolean)

result = natural_sort(data)

OUT = result``````
2 Likes

Dear Kenzo,

Christmas came early - It works LIKE A CHARM!

Thank you for your help and explanations along the way, things are all in all much clearer now then before I entered the forum and I hope to keep the learning curve steep!

You Sir have made my day so have a nice one yourself!