Why Double Sort
In Dynamo you get to deal a lot with sorting and filtering data. In many situations the core nodes are enough and you can build most of the need functionality with them. However this in some cases turns out to be extremely tedious and it removes the focus in the script from it’s actual value to the side helpers. For the functionalities that lack creating a custom node is the best option.
For this node I used python.
Let’s say you have a list of lines and you want to sort them by their length. Double sort helps you doing that.
It accepts 2 lists as arguments, they must have the same structure.
It sorts the first list (a set of primitive values – numbers in this case) and it rearranges the second list to match the sorted list
In this image you can see a set of lines that are sorted by their lengths. There are two lists, one with lines and the other with lengths. And each time length list is rearranged the lines list is brought along
The code of achieving this is not complicated, It makes use of 3 parts:
- isIterable – checks if the argument is a list or not
- validate – checks if 2 lists have the same size and this check is made recursively to all sublists
- sortLists – makes the sorting and rearranges the second list
import re #The inputs to this node will be stored as a list in the IN variable. class utils: def isIterable(self, unit): t1 = t2 = False; if(isinstance(unit, (tuple, set, list, dict))): t1 = True; if(hasattr(unit, "__iter__")): iterstr = str(unit.__iter__()); if(re.search("System.Array", iterstr)): t2 = True; return (t1 or t2); ut = utils(); L1 = IN; L2 = IN; #validation def validate(List1, List2): if(ut.isIterable(List1) and ut.isIterable(List2)): if(len(List1) == len(List2)): return True; return False; def sortLists(List1, List2): for idx in range(len(List1)): for idy in range(len(List1)): #for nested arrays if(ut.isIterable(List1[idx]) and ut.isIterable(List2[idx])): retVal = sortLists(List1[idx], List2[idx]); List1[idx] = retVal; List2[idx] = retVal; else: it1 = List1[idx]; it2 = List1[idy]; #must add check for it1 and it2, both must not be iterable #must add dynamo units comparison if(it1 < it2): swap = List1[idx]; List1[idx] = List1[idy]; List1[idy] = swap; swap = List2[idx]; List2[idx] = List2[idy]; List2[idy] = swap; return [List1, List2]; if(validate(L1, L2)): retVal = sortLists(L1, L2); else: retVal = [False, False]; OUT = retVal