PYTHON - order a list based in another list

Hello everyone,

beginners question here :grimacing:

I am trying to get a list of points (x,y,z) ordered based in the z value.
As a beginner, I thought I could use two lists (xyz and only z) to create a dictionary, then sort it, then create a new list with the sorted positions x,y,z. Why is it not working and is there another better method to achieve what I am trying to?

Cheers!

To sort by Z then X then Y use the following:

import clr
orglst = IN[0]
orglst.sort(key = lambda orglst: (orglst.Z, orglst.X, orglst.Y))
OUT = orglst

Adaption of the code by:

3 Likes

that is beautiful, and it works indeed.

As a beginner, I would like to understand what I am doing though, and this lambda expression was a bit too much. I tried to translate it into a function and was not successful. Would you mind helping me with that as well?

import clr
orglist = IN[0
def reorder(elem):
return elem.Z
orglist.sort(key=reorder)
OUT = orglist

Thanks a lot!

Loud and clear :wink:

Dialing this right back… the simplest way to sort by one part of an XYZ is to use a List.SortByFunction node. Then use the re-ordered list to get the Elements in order :slight_smile:

2 Likes

for some reason that didnt work here, but I am really curious to understand whats the mistake I am doing with the function in python. I would be interested in understanding how to translate the lambda into a function, if that makes sense? Just as a way to understand what the lambda is doing… Thanks a lot!

To sort by Z then X then Y as a function in a Python the input and return needed to be adjusted to work as a definition.

import clr

def sortbyZXY(orglst):
	orglst.sort(key = lambda orglst: (orglst.Z, orglst.X, orglst.Y))
	return orglst
	
input = IN[0]

result = []

for i in input:
	result.append(sortbyZXY(i))

OUT = result
2 Likes