Sorting data trees items using grasshopper python

Hello Guys,

I have a list containing several trees, each tree has a single data point. I need to sort them based on a specific user-defined function. How can I do it using python inside the grasshopper. I’m not sure how to manage and specify data trees inside the python window. In other words, I want to define for loops to iterate the trees {A;B;C} but IDK how?
My data is shown below:
{0;0;0}
0. {1, 1, 0}
{0;0;1}
0. {1, 2.6, 0}
{0;0;2}
0. {1, 4.2, 0}
{0;0;3}
0. {1, 5.8, 0}
{0;0;4}
0. {1, 7.4, 0}
{0;0;5}
0. {1, 9, 0}
{0;1;0}
0. {4, 1, 0}
{0;1;1}
0. {4, 2.6, 0}
{0;1;2}
0. {4, 4.2, 0}
{0;1;3}
0. {4, 5.8, 0}
{0;1;4}
0. {4, 7.4, 0}
{0;1;5}
0. {4, 9, 0}
{0;2;0}
0. {7, 1, 0}
{0;2;1}
0. {7, 2.6, 0}
{0;2;2}
0. {7, 4.2, 0}
{0;2;3}
0. {7, 5.8, 0}
{0;2;4}
0. {7, 7.4, 0}
{0;2;5}
0. {7, 9, 0}
{0;3;0}
0. {10, 1, 0}
{0;3;1}
0. {10, 2.6, 0}
{0;3;2}
0. {10, 4.2, 0}
{0;3;3}
0. {10, 5.8, 0}
{0;3;4}
0. {10, 7.4, 0}
{0;3;5}
0. {10, 9, 0}
{0;4;0}
0. {13, 1, 0}
{0;4;1}
0. {13, 2.6, 0}
{0;4;2}
0. {13, 4.2, 0}
{0;4;3}
0. {13, 5.8, 0}
{0;4;4}
0. {13, 7.4, 0}
{0;4;5}
0. {13, 9, 0}
{0;5;0}
0. {16, 1, 0}
{0;5;1}
0. {16, 2.6, 0}
{0;5;2}
0. {16, 4.2, 0}
{0;5;3}
0. {16, 5.8, 0}
{0;5;4}
0. {16, 7.4, 0}
{0;5;5}
0. {16, 9, 0}
{1;0;0}
0. {1, 1, 9}
{1;0;1}
0. {1, 2.6, 9}
{1;0;2}
0. {1, 4.2, 9}
{1;0;3}
0. {1, 5.8, 9}
{1;0;4}
0. {1, 7.4, 9}
{1;0;5}
0. {1, 9, 9}
{1;1;0}
0. {4, 1, 9}
{1;1;1}
0. {4, 2.6, 9}
{1;1;2}
0. {4, 4.2, 9}
{1;1;3}
0. {4, 5.8, 9}
{1;1;4}
0. {4, 7.4, 9}
{1;1;5}
0. {4, 9, 9}
{1;2;0}
0. {7, 1, 9}
{1;2;1}
0. {7, 2.6, 9}
{1;2;2}
0. {7, 4.2, 9}
{1;2;3}
0. {7, 5.8, 9}
{1;2;4}
0. {7, 7.4, 9}
{1;2;5}
0. {7, 9, 9}
{1;3;0}
0. {10, 1, 9}
{1;3;1}
0. {10, 2.6, 9}
{1;3;2}
0. {10, 4.2, 9}
{1;3;3}
0. {10, 5.8, 9}
{1;3;4}
0. {10, 7.4, 9}
{1;3;5}
0. {10, 9, 9}
{1;4;0}
0. {13, 1, 9}
{1;4;1}
0. {13, 2.6, 9}
{1;4;2}
0. {13, 4.2, 9}
{1;4;3}
0. {13, 5.8, 9}
{1;4;4}
0. {13, 7.4, 9}
{1;4;5}
0. {13, 9, 9}
{1;5;0}
0. {16, 1, 9}
{1;5;1}
0. {16, 2.6, 9}
{1;5;2}
0. {16, 4.2, 9}
{1;5;3}
0. {16, 5.8, 9}
{1;5;4}
0. {16, 7.4, 9}
{1;5;5}
0. {16, 9, 9}

It’s simple to convert a Data Tree to a nested list in Python using the treehelpers module.

Then both the built in function sorted and the .sort list method accept custom key functions to sort the contents in any order you like.

Thank you very much my friend. Could you please be more specific?

Have a read through here:

And here:

Edit: I see the link to the Grasshopper API broke, here’s the current documentation:

1 Like

Thank you very much Andres. To be honest, I did not really understand what is going on in the documentation. I have tried to read them but it gets more confusing. Do you know a straightforward introduction to treehelpers or how to manage trees inside python?

I appreciate your help in advance.