In the screenshot the function was called normally so it can’t take multiple frames to complete. It would run till it’s done.
Of course, that makes sense and means it should work. Thanks, my tired brain never even considered that it is actually restricted to running within that tick.
The array could be considered sorted if the sub event isn’t run once in an entire loop. You could use a variable for that: set it to 0 before the loop, set it to 1 in that sub event, then check if the var is still 0 after the loop. But then all the previous recursions would need to finish all their loops of doing nothing before it was truly done.
I tried this method and it consistently failed. Likely due to how recursive functions stack.
Another idea is to pass a depth parameter to the function When you first call the function use 0. Then each recursed call would be depth+1. Finally add another event to the function to check if depth=0, and if it is the whole function is done.
I had also tried this exact method but it failed with inconsistent results, often stopping the function before the sorting was complete. Couldn't work out why.
Personally I’d write the sort in a non recursive way. Like sorting one column at a time. If for no other reason but to be more efficient. Your function will sort but it will be looping over the array a lot.
I agree, it's not the most efficient but I can't think of a way to sort all the non zero values in a single column to the bottom without recursion.