This is certainly a problem that is more complicated than it initially appears, but it’s still fairly straightforward if you can lay out the logic of what’s happening. As long as you have a known start point, you can work from the current point to the next closest point. You just have to be sure to remove “used” points from the pool of available options after each step. That’s the part that can get a little tricky.
I’ve shown one option below that gets all the distances up front and sorts them by closest adjacent point for each of the possible locations. The python script just steps through each group of potential points and grabs the first (closest) point that isn’t already in my list of “used” points. I did this with indices instead of actual points to hopefully explain the situation a little better and make the next part easier. As you step through and grab an index (point), you use that index as the group to check next. You’re left with the index of the closest point for each of the original points as they’ve been stepped through.
orderedDistances = IN[0]
sortedOrder = [0]
i = 0
n = 0
while n < len(orderedDistances):
distances = orderedDistances[i]
for d in distances:
if d not in sortedOrder:
sortedOrder.append(d)
n+=1
OUT = sortedOrder
Sorry, I forgot to format my python code initially. Make sure the increment to n is under the while loop and not the if loop. I also noticed that you may have redundant points (your first group has two points that are 0 units away). It would probably help to clean those up first. You may also have issues if you have points that are equidistant from your current location as you may get the wrong branch.
What kind of element(s) are you working with here? If these are pipes and pipe fittings I recommend using the connectors, of each piece separately, to create a straight line for each piece. Then using the individual lines simply create a PolyCurve by those joined curves.