Sort list using Gh Python Component

Hello Everyone,

I am trying to sort the list of numbers in ascending order. I have used the Sort list component of Grasshopper and I got the desired list but when I call this component using python it give me same result but with two list. One with the required list of numbers and second one with None. Any idea why is this happening?

Thanks in advance


Do you have the .gh?

yes.
sort_list.gh (7.4 KB)

I have missing sof_on import…this looks like a gpt chat generated entry?

I haven’t used chat gpt.

Use the native Python sort and make sure to cast your data to integers, else they will sort as strings:


sort_list_Anders.gh (12.5 KB)

import rhinoscriptsyntax as rs
import Grasshopper as gh
import ghpythonlib.components as ghc

dokument = ghenv.Component.OnPingDocument().Objects

D=[]
D_sort=[]

if Aktualisieren:
    for obj in dokument:
        if type(obj) is gh.Kernel.Special.GH_Panel:
            if obj.NickName=="LF":
                D.append(obj.UserText)
    D_sort=ghc.SortList(D)
    D_sort = D_sort.keys

    for obj in dokument:
        if type(obj) is gh.Kernel.Special.GH_Panel:
            if obj.NickName==Panel_Name:
                obj.UserText='\r\n'.join(map(str,D_sort))
                obj.ExpireSolution(True)

Or you could just use Python sorting:

import rhinoscriptsyntax as rs
import Grasshopper as gh

dokument = ghenv.Component.OnPingDocument().Objects

D=[]

if Aktualisieren:
    for obj in dokument:
        if type(obj) is gh.Kernel.Special.GH_Panel:
            if obj.NickName=="LF":
                D.append(int(obj.UserText))
    D_sort = sorted(D)

    for obj in dokument:
        if type(obj) is gh.Kernel.Special.GH_Panel:
            if obj.NickName==Panel_Name:
                print D_sort
                obj.UserText='\r\n'.join(map(str,D_sort))
                obj.ExpireSolution(True)
1 Like

Thank you very much