# Closest point for given point sorting loop

**URL:** <https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600>\
**Category:** Lists-Logic\
**Created:** [June 20, 2017, 2:31pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600 "2017-06-20T14:31:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![maselko](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@maselko](https://forum.dynamobim.com/u/maselko)\
**Post date:** [June 20, 2017, 2:31pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/1 "2017-06-20T14:31:37Z")

</div>

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.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/9/592ddcdfcd431c36f26cb1f29efa7c08b26f765b.png)

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

Ty for help!.

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [June 20, 2017, 11:21pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/2 "2017-06-20T23:21:20Z")

</div>

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

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/9/49573496164f7c0ce40834daba2f06f53c4db052.PNG)

---

<div class="post-metadata">

**Author:** ![maselko](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@maselko](https://forum.dynamobim.com/u/maselko)\
**Post date:** [June 21, 2017, 7:05am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/3 "2017-06-21T07:05:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [June 21, 2017, 10:09am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/4 "2017-06-21T10:09:26Z")

</div>

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:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/0/904618f3517ae0d84a236f12f43e1caaa1c8d784.png)

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

---

<div class="post-metadata">

**Author:** ![maselko](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@maselko](https://forum.dynamobim.com/u/maselko)\
**Post date:** [June 22, 2017, 7:19pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/5 "2017-06-22T19:19:43Z")

</div>

After a few infinite loop crashes I got this:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/d/bd5b1e2f816ae8e47415f02ecb2f2b42638207c1.JPG)

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](https://forum.dynamobim.com/uploads/short-url/jHRYcTw56mCtHSQDZO9fiCljTXD.dyf) (8.7 KB)  
Thank you for help!

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [June 23, 2017, 6:05am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/6 "2017-06-23T06:05:32Z")

</div>

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.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/2/6249be7f0d4a9846ada01790fc8f15a9c1258241.png)

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](https://forum.dynamobim.com/uploads/short-url/gH0FN5wx6MVBB4lmbuCKIBWXITT.dyf) (8.4 KB)

---

<div class="post-metadata">

**Author:** ![ruben.rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@ruben.rodrigues](https://forum.dynamobim.com/u/ruben.rodrigues)\
**Post date:** [September 8, 2020, 2:13pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/7 "2020-09-08T14:13:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [September 8, 2020, 5:03pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/8 "2020-09-08T17:03:55Z")

</div>

You could try using this Design Script option instead

 ![closestPointsSequence_2020-09-08_10-14-58](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/f/1fe6bd834e063afd940fb13f8b2a202cfcb43eeb.png)  
[closestPointsSequence.dyn](https://forum.dynamobim.com/uploads/short-url/eZnysmQPBrRMyTzM196KTVxFDub.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;
	}
};
```

---

<div class="post-metadata">

**Author:** ![ruben.rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@ruben.rodrigues](https://forum.dynamobim.com/u/ruben.rodrigues)\
**Post date:** [September 14, 2020, 12:01pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/9 "2020-09-14T12:01:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ruben.rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@ruben.rodrigues](https://forum.dynamobim.com/u/ruben.rodrigues)\
**Post date:** [September 14, 2020, 12:06pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/10 "2020-09-14T12:06:54Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [September 14, 2020, 12:17pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/11 "2020-09-14T12:17:16Z")

</div>

> [@ruben.rodrigues](#):
>
> In my case i will have multiple list (different lists of points) that i need to sort by the closest way

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

 ![closestPointsSequence_2020-09-14_05-45-18](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/2/82e041a96d2e77742307a535d7de77d08cda09da.png)  
[closestPointsSequence.dyn](https://forum.dynamobim.com/uploads/short-url/arZPmfduMvDQqCGnDnQD1Kzqpzs.dyn) (10.8 KB)

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [September 14, 2020, 2:20pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/12 "2020-09-14T14:20:53Z")

</div>

> [@ruben.rodrigues](#):
>
> In my case i will have multiple list (different lists of points) that i need to sort by the closest way

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

 ![shortestPathConnectingPoints_2020-09-14_07-48-45](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/b/ab2db838964bf15e2b80fe714af0165e1e6610e1.png)  
[shortestPathConnectingPoints.dyn](https://forum.dynamobim.com/uploads/short-url/cbCFMZDukhPNmzS5aWn3QZtf6Fx.dyn) (14.7 KB)

---

<div class="post-metadata">

**Author:** ![simon\_g](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@simon\_g](https://forum.dynamobim.com/u/simon_g)\
**Post date:** [June 2, 2022, 6:16am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/13 "2022-06-02T06:16:50Z")

</div>

This is exactly what I was looking for…however…

Using Revit 2022/ Dynamo 2.12, the Design Script for sorting points by proximity fails - returns the first point and the rest are nulls.  
Working in Revit 2020 (whatever Dynamo version that is) it works completely fine.

With my limited knowledge of scripting, the logic seems fine, so I’m assuming this is due to a change in Design Script interpretation, but I could not figure it out. Any hints on what has changed?

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [June 2, 2022, 7:28am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/14 "2022-06-02T07:28:33Z")

</div>

Shouldn’t be any changes; are you sure the data sets are the same in both files?

---

<div class="post-metadata">

**Author:** ![simon\_g](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@simon\_g](https://forum.dynamobim.com/u/simon_g)\
**Post date:** [June 2, 2022, 11:28pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/15 "2022-06-02T23:28:31Z")

</div>

Yep, data sets are the same. I even downloaded the example from Dimitar above (first one) and got the same erroneous result.

The only other thing I can think of is perhaps a package error somewhere in my 2022 build?

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [June 3, 2022, 12:22am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/16 "2022-06-03T00:22:27Z")

</div>

Can you post a screenshot of the failing node? May be as simple as adding “Autodesk.” or “DSCore.” in front of a method which isn’t showing as a failure because of the in-line code.

---

<div class="post-metadata">

**Author:** ![simon\_g](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@simon\_g](https://forum.dynamobim.com/u/simon_g)\
**Post date:** [June 3, 2022, 12:54am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/17 "2022-06-03T00:54:46Z")

</div>

This is what I’m getting. Points are created fine, but when calling the design script, first point only is collected.

 ![closestPointsSequence(2)_2022-06-03_10-51-07](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/7/17fe626cfbf5813b91f95c63a57669bbbef40e4b.png)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [June 3, 2022, 2:18am UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/18 "2022-06-03T02:18:55Z")

</div>

Hmmm… I’ll try and take a look at that tomorrow. 🙂

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [June 3, 2022, 3:54pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/19 "2022-06-03T15:54:21Z")

</div>

Change _“sorted list”_ on line 14 to _“sortedList”_

---

<div class="post-metadata">

**Author:** ![simon\_g](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@simon\_g](https://forum.dynamobim.com/u/simon_g)\
**Post date:** [June 5, 2022, 11:20pm UTC](https://forum.dynamobim.com/t/closest-point-for-given-point-sorting-loop/12600/20 "2022-06-05T23:20:16Z")

</div>

That did it. Thanks a heap Vikram!  
Any hints as to why the original worked in prior versions, but the change is needed in Dynamo 2.12?  
tbh I’m not really sure what that part of the line is doing?
