Sorted points list by X coordinate and then by Y coordinate

Hi, I have a points list and I want to sort it by X coordinate first and then by Y coordinate.
If I use the “sort list” component I only able to sort it according to X coordinate or to Y coordinate.
Someone can help me?
Thanks. (27.7 KB)


image (26.8 KB)

1 Like

@martinsiegrist Thank you… Do you also know how to do it with gh python??

Sorry, no.

You can also do this with the Sort Points component.

This shows how to do the same thing with a GhPython or C# script. (15.4 KB)


1 Like

Sorting with @kev.r solutions seem to fail on the points provided further above.

Using my Grasshopper sorting above and string formatting, the coordinates look like this:

Looks like the values provided should be rounded before sorting: (30.1 KB)

1 Like

I usually do something like this in Python:
(Edit* I made a small change) (8.4 KB)

1 Like

To use Sort Points, you could also round in Python.

import rhinoscriptsyntax as rs
rnd = 3

npnts = [rs.AddPoint(round(i.X,rnd),round(i.Y,rnd),round(i.Z,rnd)) for i in pnts]
npnts = rs.coerce3dpointlist(npnts)

a = rs.SortPoints(npnts, True,0)
1 Like

I usually use this pattern when sorting by an object property:

Yes, old trick. You can round point coordinates to any precision using an expression: round(Pt,P) (5.7 KB)


Translate this (in C#): pList = pList.OrderBy(x=>x.X).ThenBy(x=>x.Y).ToList();

ot for rounded coords use: (x=>Math.Round(x.X, decimals)) , (x=>Math.Round(x.Y, decimals))

1 Like

By the way, you can change the way points are sorted like this (Y, X, Z): (8.7 KB)

Or sort by Z first, then Y, then X.

1 Like