Grasshopper Python - How? RhinoList<T>.Sort Method (Double[])

I’m trying to script ‘Sort,’ but it’s not working, what am I doing wrong?
Thanks in advance for your response.

RhinoList.Sort Method (Double[])

20180916 00 sort values.gh (13.9 KB)

How to get only a ‘key’ from ‘sort’ and use the ‘key’ for sorting another list.

I know this will work, but not completely the way I wantend. With this the list has changed and I cannot retrieve the former list nor do I obtain a ‘key’ from it.

import rhinoscriptsyntax as rs
import Rhino as rc

listVa = x
listVa.sort()
print(listVa)

How it is meant to type the part below? I do not need the Namespace part?
RhinoList.Sort Method (Double[])

I am trying to sort based on a key in another way. But, I cannot figure it out.
20180916 01 sort values.gh (12.5 KB)

Do you might know how to type a ‘key’ in the ‘sort method’ of Python, Python Rhino, and Python rhinoscriptsyntax?

1 Like

Nice, thank you. Stil I am doing something wrong, do you know what I am doing wrong?

20180916 02 sort values.gh (12.0 KB)

Hi @ForestOwl,
I can’t really help to sort an entire list, but in case if you want to find indexes of min and max values and retrieve them, please have a look min_max_by_index.gh (7.6 KB)
.


Good luck with your GH python definition!
Ilja

1 Like

You need to define the lambda function to sort by, like so:

20180916 02 sort values_AHD.gh (2.1 KB)

1 Like

So nice, thanks! I assume you mean with ‘define lambda’ the part of ‘pt: pt.DistanceTo(itemPt),’ am I understanding it right?

If so, I than understand that ‘lambda pt:’ including the colon are connected. So, ‘lambda and the colon’ are a thing? I confuse it with the = sign. Why could the colon not be an = sign?

So, ‘key = lambda pt:’ is a thing (which can be found ‘somewhere’ in ‘some manual,’ which I could not find)?

And, in that manual, are there other options shown like lambda?

I do not intend you to ask where I can find the manual, but only ‘how’ you could knew about that piece of magic.

Experience, Googling, and always ALWAYS be reading the docs

2 Likes

Somehow, I cannot order other lists like the indices through ‘cpm’ with the same key. Do you know how that works?

20180916 05 sort values.gh (12.7 KB)

Hello,
I think this,
SortedList=sorted(enumerate(Points), key=lambda (i,pt):pt.DistanceTo(itemPt))
(index,pt)=SortedList[0] #unpack
print index

or this.
SortedList=sorted(zip(range(len(Points)),Points), key=lambda (i,pt):pt.DistanceTo(itemPt))
(index,pt)=SortedList[0] #unpack
print index

Tuple, Unpack tuple, enumerate and zip are all cool functinos that Pyhton provides.

2 Likes

ありがとう ! Arigatō!
Great Mikity, thank you!

Hmm… in this scenario I want to obtain the indices sorted by the sorted form of the Data list. I am not able to solve it. Do you know what I am doing wrong?

20181020 sort values 06.gh (7.6 KB)

Hi ForestOwl

You wrote less “lambda expressions” like this.


import rhinoscriptsyntax as rs

dist_listIxVa = [(i, v) for i, v in enumerate(dist_listVa)]
sorted_dist_listIxVa = sorted(dist_listIxVa, key = lambda float: dist_listVa[i])
1 Like

Also, maybe avoid naming variables/functions float, as this is one of the built-in Python functions (which you risk overwriting within the scope of your script).

1 Like

Thank you for your responses.

dist_listIxVa = [(i, v) for i, v in enumerate(dist_listVa)]
sorted_dist_listIxVa = sorted(dist_listIxVa, key = lambda (i,v): dist_listVa[i])

I forgot the (i,v) after lambda too. :slight_smile:

Hello

dist_listIxVa = [(i, v) for i, v in enumerate(dist_listVa)]
sorted_dist_listIxVa = sorted(dist_listIxVa, key = lambda (i,v): dist_listVa[i])

There are two mysterious things.

  1. dist_listVa[i] in the second line is obviously v.
  2. [(i,v) for i,v in enumerate (dist_listVa)] is exactly same as enumerate(dist_listVa) (or maybe technically different but that’s that…)

So, I guess this is what you intended to do…
sorted_dist_listIxVa = sorted(enumerate(dist_listVa), key = lambda (i,v): v)

Try it out.

1 Like

Thank you! It worked and understand enumerate better know.

I was trying to script it like that with the v at the end. Now I finally understand how it works correctly. :smiley: And understand Python better. Yes! Thanks!