Syntax problem of For / While loops or Imperative block

Brilliant! The concept of [associative] and [imperative] methods is still new to me, so that really helps me. Thank you! (Tack så mycket!) :man_bowing:

Likely your Line.StartPoint(remainingLines); and remainingLines.StartPoint are both failing due to number 1

Yes, that was causing the problem. I can that see your solution replaces them with For loops.

I ran and referenced your code. The annotations really helped. :grinning_face: Below is the code that I will adopt. It is basically that same a yours with a small adjustment to change the line direction.

def SortLinesByNearest(
    curves: DesignScript.Curve[],
    startPoint: DesignScript.Point)
{
    return = [Imperative]
    {
        unprocessedLines = curves;
        lineCount = DSCore.List.Count(unprocessedLines);
        currentEndPt = startPoint;
        sortedLines = [];

        for (index in 0..(lineCount - 1))
        {
            nearestDists = [];
            boolReverseLine = [];

            for (candidateLine in unprocessedLines)
            {
                distToStartPt =
                    Autodesk.Geometry.DistanceTo(candidateLine.StartPoint, currentEndPt);

                distToEndPt =
                    Autodesk.Geometry.DistanceTo(candidateLine.EndPoint, currentEndPt);

                boolStartCloser = distToStartPt < distToEndPt;
                nearestDist = boolStartCloser ? distToStartPt : distToEndPt;

                nearestDists = List.Join([nearestDists, nearestDist]);
                boolReverseLine = List.Join([boolReverseLine, boolStartCloser]);
            }

            linesSortedByDist =
                List.SortByKey(unprocessedLines, nearestDists)["sortedList"];

            reverseBoolSortedByDist =
                List.SortByKey(boolReverseLine, nearestDists)["sortedList"];

            closestLine = linesSortedByDist[0];

            orientedLine =
                reverseBoolSortedByDist[0]
                ? closestLine
                : closestLine.Reverse();

            sortedLines =
                DSCore.List.AddItemToEnd(orientedLine, sortedLines);

            currentEndPt = orientedLine.EndPoint;
            unprocessedLines =
                DSCore.List.DropItems(linesSortedByDist, 1);
        }

        return = sortedLines;
    };
};

I also did some speed tests with TuneUp to decide which of the following methods to adopt for my code. As you can see the difference is minimal:

  • For < While : approx. 5-15 ms less processing time
  • List.SortByKey < List.IndexOf : approx. 10 ms less processing time