Sort Points by Y in Python

python

#1

Hi all,

how can a pointlist be sorted by Y Value?

Thanks


#2

There are a number of ways, you could use rs.SortPoints() or something like the following:

#(assuming pts is a list of 3D points)
pts_sort_y = [item[1] for item in sorted([(pt.Y,pt) for pt in pts])]

#3

I shared two of the methods I tend to implement here:


#4

Thanks for the examples.

the List comprehension method is nice.


#5

You don’t need the nested loop/list comprehension, can get there with just sorted():

181011_SortPointsByY_00.gh (4.1 KB)


#6

Thanks a lot for this example…

i have a lot of problems to understand the key in the sort method.
Your example is easy to understand.


#7

The key parameter defines what to sort by and the lambda is just an inline function (in this case, one that returns the Y component of a point). One can usually find good sources on Google for general Python patterns (like sorting etc): https://wiki.python.org/moin/HowTo/Sorting