Sort Points by Y in Python



Hi all,

how can a pointlist be sorted by Y Value?



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])]


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


Thanks for the examples.

the List comprehension method is nice.


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


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.


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):