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.
#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.
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?
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.
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.
# Load the Python Standard and DesignScript Libraries
import sys
import clr
import re
clr.AddReference('ProtoGeometry')
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
clr.AddReference('ProtoGeometry')
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
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!