C# script - sort index based on closest distance

Hi! I am a beginner writing c# script and it takes me forever to figure out a simple function.
I would really appreciate for your input!

I wish to sort index numbers based on closest distance. As like the screenshot by using vanilla grasshopper components. However I wish to use ‘for’ loop and run this function automatically with c# script (because I do not know python) for further development.

I tried to write the script by myself but the quality is so shameful so I am not even dare to publish it here. The most difficult part was I do not know how to get the start/end point from the shortest line. I searched Rhinocommon API and when I use pt.PointAt(0) method, it takes point(0,0,0) not the starting point on the specific line.

Hope I made clear explanation. Thanks for your advice!!

This IS a good thing (if you are in the broad AEC market segment).

Is the attached what you are after?

Sort_ToClosestLINQ_V1B.gh (14.5 KB)

1 Like

so cool! I I learned a lot from you script! so many new methods :sweat_smile:

BTW: For a given Point3d pStart = pList[index] and using a clone cpList = pList.ToList() … cpList = cpList.OrderBy(x=>x.DistanceTo(pStart)).ToList(); sorts the clone. Then foreach(Point3d p in cpList) int sortedIndex = pList.IndexOf( p )

1 Like

By using that function, I can sort points by distance, am I right? thanks!

Indeed. PS for the moment is better to use that type of stuff:

Sort_ToClosestLINQ_V1.gh (7.5 KB)

Or

Sort_ToClosestLINQ_V1A.gh (17.0 KB)

1 Like

it is so clean! previously I drew lines between points, made a double type list to store length, and then use if(value<min) function to compare all value blah blah blah… but it is so simple and neat by using sortedPts.OrderBy(x => x.DistanceTo(start)).ToList() function! hahaha!!!