Sort List with lambda fuction

Hi! I try to sort the list “point0” defining a lambda fuction, so like:


But it’s not working. Anyone know how I can do?
Thank’s.

Hello,

You should give a callable as the key to your sorted method so if I understand your script correctly the parameter should be key=lambda pt: pt.DistanceTo(itemPt0) note the capitalised To

Note that you can also sort the list in place by calling point0.sort(key=... to avoid creating a duplicate list, if that is useful?

-g

I don’t understand lines 37 (incomplete) and 38 (I dont think there is anything to unpack - your list contains Point3d objects, no?)

Sorry, I rewrite the script

Did you see my other message above :slight_smile: :grey_question:

oh yes! I try with “point0.sort(key=lambda pt: pt.Distanceto(itemPt0))” but it’s not work equally.
Sorry I’m beginner with Python.
What do you advise me to do?

Wtih capitalised To in DistanceTo ? Do you get an error message? Can you copy the precise error message here?
Can you create a minimal file with geometry internalised to share to demonstrate the problem?

This is the error:

sort list.gh (30.8 KB)

OK please reread the error message and my replies above. You have the answer in front of you :wink:

Here

You have used lower case

A OK! but I have the same problem with “DistanceTo”

OK - sorry my turn not to read properly - you have ‘guid’ objects not point3D. try key = lambda pt: rs.Distance(pt, itemPt0) ???

rhinoscriptsyntax generally returns guid references to objects, not the objects themselves. This makes it a bit tricky combining rs and Rhino functions - you do have a bunch of ‘coerce’ functions which wiil help you work your way across the boundary where needed, e.g. coerceguid, coerce3Dgeometry, …

Yess!!! it works!!
Thank you so much!