# Random curve selection order results in unsortable list

Hi everyone,

i have created a small python script that takes a bunch of input curves, get’s their midpoints and then sorts these in ascending or descending order on a user chosen axis. Quite handy to figure out which is the top curve, bottom curve etc.
It works well if the selection order of the curves is linear (e.g. left to right, right to left) but if I chose curves in a random order it stops working and returns a random list again.

Could anyone please check it out? There is a attached Rhino file with curves that are spread out on the x axis and the gh file with the components in question.
Below is the python code and an image showing the setup. X=0 axis, if you connect “select from left to right” or “select from right to left” to the ghpython inObjects it will work in both directions both with Biggest first set to True and False… If you connect “random select” it does not…
Any ideas? I really do not get what is wrong with it

`````` if objectType == 4:
#Get the midpoints of the input curves
for c in inObjects:
midPoints.append(rs.CurveMidPoint(c))
#deconstruct the midpoints and get values for the specified axis
for point in midPoints:
axisVals.append((point[axisNr]))
#Make a copy of the list
sortedVals = list(axisVals)
print(axisVals)
#sort sorts by ascending order by default
if maxFirst == True:
sortedVals.sort(reverse=True)
if maxFirst == False:
sortedVals.sort(reverse=False)
print(sortedVals)
#Find every value in the sorted list and store an index order
for val in axisVals:
indices.append(sortedVals.index(val))
print(indices)
#Resort the curves to the new order based on the indices
for i in indices:
sortedObjects.append(inObjects[i])
outObjects = sortedObjects
``````

randomSelectionOrderNotSortable.3dm (29.4 KB) randomSelectionOrderNotSortable.gh (10.6 KB)![curvestopview|690x286]

I think you need to change this line

``````for val in axisVals:
indices.append(sortedVals.index(val))
``````

to

``````for val in sortedVals:
indices.append(axisVals.index(val))``````
1 Like

Hi Gijs,
thanks for your idea :). That did not do the trick :(.
The idea in those lines is to get the index from the sorted list for every item from the original list. SortedVals contains the sorted values, so getting the index of the original value in the sorted list should get me the target index for where the item needs to be. I do not think the mistake is there because then it would also not work on the two sequentially selected curve lists.
If you have any other idea, please let me know . Thank you!
Best regards,
M

strange… seems to work here, or I don’t get what you are after:

randomSelectionOrderSortable.gh (17.6 KB)

Hi again Gijs,
tried it again and it seems to work the way you said now… Must have been pretty late that other night :-/.
Kind of still don’t get why it does not work the other way around though, getting the index from the sorted array by asking for the original value should yield the index of the original element in the sorted list…
Thanks a lot! .

Best regards,
M