Closest point for given point sorting loop

Hi dynamo users. I stucked with idea of loop that can sort the points by distance but not from just one point:

->Starting with starting point and point collection,-> then find closest point to the starting then take the found one and repeat the search of closest one. Then sort it in found order.

I tried with loop while node after I created custom node that giving the closest point and rest of collection.

Ty for help!.

See if this works for you. Doesn’t require the looping.

1 Like

Ty for reply, I tried it, but its still one problem. We are picking only first control point, next ones should be found, thats why im was trying with loop. It will be used for reconnecting bad quality dwg polylines when is alot of trash aroud. I dont know before starting the script with points will be next in the order. Thats the issue not sorting.

The LoopWhile node is hard to use because you’re limited to a single input. However, that input can be a list or a list of lists. Once you realize that, its use becomes much easier:

It’s more fun if you discover the approach yourself, so I’ll just post the above as an example for now.

1 Like

After a few infinite loop crashes I got this:

The sorting node works fine as alone but with the same data connected to loop I’m getting only one item, wich is should be the last one. What is the difference when is connected? And I dont get how You Dimitar getting same preparing data result as I’m when using one List.Create node less.

The closest sorting node is attatched.
closest sorting 27.dyf (8.7 KB)
Thank you for help!

In your case you’re using the the list syntax in your code block - {0} generates a list with a zero. When you feed a list into a node that expects a singleton, the interpreter detects that and outputs a list as well.

Looks like you’re almost there! Your node is replacing the closest point with the previous one, where as my example was appending it to the end of the list.

_tempSortPt.dyf (8.4 KB)

1 Like

Hello guys,

I’ve tried do something similar but considering multiple lists of points.

When i run the script without the LoopWhile I get the correct Sort but once i put the node LoopWhile the Script simplt doesn’t respond…

I’m not authorized to attach files as new member but i can send you an e-mail with the script and custom node that I’ve created to see if one of you can help me out.

You could try using this Design Script option instead

closestPointsSequence.dyn (8.7 KB)

def ClosestPointsSequence (stPt, pnts:var[]..[])
	return = [Imperative]
		cnt1 = List.Count (pnts);
		pnt1 = pnts;
		pnt2 = [stPt];

		while (List.Count(pnt2)<cnt1)
			pnt3 = List.SetDifference(pnt1,pnt2);
			pnt4 = List.LastItem(pnt2);
			dis1 = pnt4.DistanceTo(pnt3);
			pnt5 = List.SortByKey(pnt3,dis1)["sorted list"];
			pnt1 = List.RestOfItems(pnt5);
			pnt2 = List.AddItemToEnd(List.FirstItem(pnt5),pnt2);

		return pnt2;

Hello Vikram,

Your suggestion works very well, but only when you have just one list because when you have list of list the Design Script doesn’t work…

I’m going to try adapt the script for a list of lists…

Thank you one more time

@Vikram_Subbaiah, just to clarify:

In my case i will have multiple list (different lists of points) that i need to sort by the closest way… I can’t do it one by one … i want to choose different origins and one destiny (for example…) and generate the closest path for every origin… in the end i will generate conduits for every path.

Use Replication Guides like this …
ClosestPointsSequence (sp<1>, pnts<1>)

closestPointsSequence.dyn (10.8 KB)

You want the shortest path connecting all points (in multiple lists of points)?

shortestPathConnectingPoints.dyn (14.7 KB)

1 Like