Designscript Loop - List Sorting

Hi, I want to sort a dataset of nested lists, similar to that in excel and revit schedules where you can select the first column to sort by, and then the next, and the next etc. I can do this with out of the box nodes, using the List.SortByKey but rather than repeat this I want to wrap it up in a loop in ds. Id like to feed in a list of numbers in this case {1,2,3} but these could be in any order and sort the list based on this. Please see example below:

Could you paste the block of code? Thanks.

I’d try a nested function:

def innerSort(l1:var[][], x:var){
    keys = List.GetItemAtIndex(l1<1>,x);
    return = List.SortByKey(l1, keys)["sorted list"];

def msort(l1:var[][], order:var[]){
return = [Imperative]{
    for (i in order){
        l1 = innerSort(l1,i);
    return = l1;

also you get minus points for not copying your code :stuck_out_tongue:

Hi @MarkDT - I was looking at your CBN and saw that you were trying to use list@level syntax and replication guides inside an Imperative Code Block. This is not currently allowed in Designscript as list@level and rep guides require a different parser. While we have discussed enabling this in the future, for now, you must use other alternatives within imperative code to get the same result.

Since, it looks like @Dimitar_Venkov solved your problem, I thought I might provide a few more tips to help with the writing of imperative code since we have limited documentation on it :slight_smile:

  • if you want your function to return what is in the imperative code block, you will need to have
    return = [Imperative] instead of s = [Imperative]

  • if you want to use lists of varying dimensionality set your parameter type to var

  • for your ‘range’ variable, you may want to start with 0. if you do that, you can then just use ‘r’ instead of making a separate variable called ‘ind’ that you have to increment. The for-in statement you have in your imperative code block will increment automatically.

  • I’ve added a simplified example below of how to increment through an input list and push new values into a new list. I hope this helps…

def myFunc(x:var[]..[]){
	newList = {};
	return = [Imperative]
		for(i in 0..Count(x)-1)
		newList[i] = x[i]+2;
		return = newList;