Ordering a list

In regards to sorting algorithms:

I am trying to sort a list of numbers in descending order, and keep track of what order the items were in.

Example:
List1=[0.1, 2.3, 0.7, -0.5] and consists of entry elements [0,1,2,3] (I will call this Array1).
Once sorted in descending order, List2=[2.3,0.7,0.1,-0.5] with the entries ordered as [1,2,0,4] (I will call this Array2).

While List2 is good to have, I am particularly interested in knowing the order of the entries (Array2). My list is paired to a larger data set, and knowing the order will allow me to access the data set in desired order using the entry indexes.

I haven’t found any sorting algorithms in the UR teach pendant interface. Is there a bubble sort function hidden somewhere, or do I need to write it myself?

Hi, did you find a solution? I’m searching for the same function.
Thanks

Hello,
There is no sorting functions “pre defined” in PolyScope or even in URScript. You have to build your own function f.ex. in script and place it in before start sequence - then you will be able to call it in main program when you’ll be need it. See Script manual for some help with creating a functions.

Hope it helps.
Wojciech

Script manual:
scriptmanualG5_.pdf (920.0 KB)

@phillip.g.grambo , @Searcher

Hello, thanks for your reply,

I managed to create a sorting function using as base a python code that I found on the internet, so I’ll leave my solution here for anyone that needs it

# Considering the array as 
array = [struct(i=0, value=3), struct(i=1, value=75), struct(i=2, value=15)] 
# Sorting the array and its indexes in descending order 
i=0 
while (i < array.length())
   j=i+1 
   while (j < array.length()): 
      if (array[i].value <= array[j].ang):
         temp = array[i] # temporary variable 
         array[i] = array[j] 
         array[j] = temp
      end
      j = j+1
   end
   i = i+1
end

If you need to sort it in ascending order you only have to change “<=” to “>=”

1 Like