Grasshopper

algorithmic modeling for Rhino

# Sorting Points by x- and y- values

I'm looking for a way to sort a list points, built up from series of x and y values, by the y-values first and then, by x-values:

Current List: Output from Point XYZ Component

(0.0, 0.0, 0.0)

(1.0, 0.0, 0.0)

(2.0, 0.0, 0.0)

(3.0, 0.0, 0.0)

(0.0, 1.0, 0.0)

(1.0, 1.0, 0.0)

(2.0, 1.0, 0.0)

(3.0, 1.0, 0.0)

(0.0, 2.0, 0.0)

(1.0, 2.0, 0.0)

(2.0, 2.0, 0.0)

(3.0, 2.0, 0.0)

Desired List:

(0.0, 0.0, 0.0)

(0.0, 1.0, 0.0)

(0.0, 2.0, 0.0)

(0.0, 3.0, 0.0)

(1.0, 0.0, 0.0)

(1.0, 1.0, 0.0)

(1.0, 2.0, 0.0)

(1.0, 3.0, 0.0)

(2.0, 0.0, 0.0)

(2.0, 1.0, 0.0)

(2.0, 2.0, 0.0)

(2.0, 3.0, 0.0)

I was unable to make the Sort List Component work successfully:

Thanks,

Amanda

Views: 10906

### Replies to This Discussion

Hi Amanda,

Sort only works on numbers. One number can be smaller or larger than another, but one point cannot really be smaller or larger than another point.

What you need to do is convert your points into numbers. Since you want to sort first by x, then by y (if x is identical), then by z (if x and y are identical), you need to somehow munge these three coordinates into a single number.

Now, if you can be certain that the coordinates are not excessively large or small numbers, then you can do something like the following:

sortkey = 10,000 * x + 100 * y + z

Then you Sort the list of sortkeys while synchronously sorting your original pointlist:

--
David Rutten
david@mcneel.com

David,

Thank you

This is great! Thank you!

Could you please repost the image? I'm able to see a portion of it?

Where the sort component sorts in the xyz.

I need to be able to sort in the yxz.

Thanks

Hi David,

All different with what I wanna do.
pls check below:

listPts = rs.coercePoints3d(input)
#is it possible use sorted() function like:
sorted(listPts, key = listPts.x)
#the syntax is bad but for address the idea that: I want use sort() or sorted() function to sort a point3d list by its x or y value.

This is a very old post, perhaps prior to the 'Sort Pt (Sort Points)' component?  Because using that, this question can be answered differently:

Attachments:

This is just genious, neat and simple

Thank you

Hello Everyone,

While investigating the same question, I worked out an alternative strategy,

01-Sort by Y location >>>

02-Make a list for each point with the same Y >>>

03-Sort Each list by X location

Its worked for us,

Cheers.

Thanks for this.

by navneet

by navneet

by navneet

• View All