# [SOLVED] Reordering points in a tree according to a new index

Hi,
I’m trying to reorder the points in a tree according to another order (always coming with the same tree structure).

I will add some info to better explain the issue.

After performing a split on a surface to get quadrangular faces the order of the list of the points for each rectangle is not the same. I tried a sort along a curve for each point in order to get the right order for all the faces, but that is not making the difference and the points for some reason are not in the same order, probably because they are in the right direction but not starting always from the same corner.

So basically I calculate the relative position of the points ( of a rectangle ) to the normal plane and then I use the signs (of the x and y coordinates) to get a new order in python to get the a new list with order [ 0,1, 2, 3 ] .
it works pretty good except for those with a strange order, like the one in red in the image

I post the code of the first component taking the relative coordinates of the point to create a new order list:

``````import ghpythonlib.components as gc
import Grasshopper.DataTree as gh_tree #to make operations
import Grasshopper.Kernel.Data.GH_Path as gh_path #to create the tree path
import Rhino.Geometry as rg
import math as m
import System

new_tree = gh_tree[System.Object]()

### the key values for sorting
temp_list=[]

###### this creates a list of orders ########

for i in range(tree.BranchCount):

path = tree.Path(i)

#########  this iterate through the Elements ########
for j,b in enumerate(tree.Branch(i)):

### create a new list of indexes

#### 0 #####
if x.Branch(i)[j]>=0 and y.Branch(i)[j]<=0:

#### 1 #####
if x.Branch(i)[j]>=0 and y.Branch(i)[j]>=0:

#### 2 #####
if x.Branch(i)[j]<=0 and y.Branch(i)[j]>=0:

#### 3 #####
if x.Branch(i)[j]<=0 and y.Branch(i)[j]<=0:

``````

and the second part where trough list comprehension a new tree is created with a new order :

``````import ghpythonlib.components as gc
import Grasshopper.DataTree as gh_tree #to make operations
import Grasshopper.Kernel.Data.GH_Path as gh_path #to create the tree path
import Rhino.Geometry as rg
import math as m
import System

new_tree = gh_tree[System.Object]()

temp_list=[]

for i in range(tree.BranchCount):
path = tree.Path(i)
temp_l = [ tree.Branch(i)[j] for j in order.Branch(i)]
``````

I hope the problem is more clear now, I am pretty sure there are better ways of doing it, in case you have other solutions please let me know.

thanks

ok, I solved it using the sorted function.

here the code:

``````import ghpythonlib.components as gc
import Grasshopper.DataTree as gh_tree #to make operations
import Grasshopper.Kernel.Data.GH_Path as gh_path #to create the tree path
import Rhino.Geometry as rg
import math as m
import System

new_tree = gh_tree[System.Object]()

temp_list=[]

for i in range(tree.BranchCount):
path = tree.Path(i)

ordered_list = [x for _,x in sorted(zip(order.Branch(i),tree.Branch(i)))]