sorting a list

40 views
Skip to first unread message

Patrick Dufour

unread,
Oct 26, 2021, 3:47:59 PM10/26/21
to Glowscript Users
I have a bunch of pairs of points that looks like this: data = [[89.9583, 112.62], [84.0692, 112.831], [78.1725, 113.479], [72.2575, 114.544], [66.3062, 115.993], [60.2906, 117.779], [54.1706, 119.846], [47.893, 122.122], [41.3925, 124.525], [34.5948, 126.958], [27.425, 129.309], [314.68, 127.196], [309.017, 123.498], [304.284, 119.31], [300.439, 114.725], [297.422, 109.832], [295.176, 104.708], [293.651, 99.4211], [292.812, 94.0335], [292.638, 88.6029], [247.357, 88.6799], [247.201, 94.1111], [246.379, 99.5013], [244.872, 104.793], [242.642, 109.924], [239.641, 114.826], [235.809, 119.422], [231.086, 123.624], [225.428, 127.336], [152.499, 129.487], [145.319, 127.122], [138.516, 124.675], [132.014, 122.256], [125.736, 119.963], [119.618, 117.878], [113.604, 116.073], [107.655, 114.605], [101.742, 113.52], [95.847, 112.852]]

I need to sort that list with the first number of the pair from lowest to highest. Is there a way to do that directly in glowscript or I need to make my own def the old fashion way?

sorted(data) kind of does it but not correctly (maybe I do not use it properly, but I find no documentation).


Patrick Dufour

unread,
Oct 26, 2021, 9:45:49 PM10/26/21
to Glowscript Users
Ok, found a quick way. I post it here in case it may help someone someday. In short, I made a list with the x values, sorted it and kept the original index (now sorted) in a list called ind. With this, I can now get all the values I want by using the stored index listed in ind.

Here is the code (slightly modified to keep index from https://stackabuse.com/sorting-algorithms-in-python/)

def selection_sort(nums):
    global ind
    ind=[]
    for i in range(len(nums)): ind[i]=i  # initial index
    # This value of i corresponds to how many values were sorted
    for i in range(len(nums)):
        # We assume that the first item of the unsorted segment is the smallest
        lowest_value_index = i
        # This loop iterates over the unsorted items
        for j in range(i + 1, len(nums)):
            if nums[j] < nums[lowest_value_index]:
                lowest_value_index = j
                         
        # Swap values of the lowest unsorted element with the first unsorted
        # element
        nums[i], nums[lowest_value_index] = nums[lowest_value_index], nums[i]
        # Swap index
        ind[i], ind[lowest_value_index] = ind[lowest_value_index], ind[i]

# Verify it works
random_list_of_nums = [12, 8, 3, 20, 11]
print(random_list_of_nums)
selection_sort(random_list_of_nums)
print(random_list_of_nums)
print(ind)

For those interested on why I want to do that: I need to do a cubic spline interpolation on those data, but the x values needs to be sorted for this to work. Yes, I made my own cubic spline def. All this would be much easier directly in python, but I want my code to work in glowscript. If there is a smarter way to do it, please let me know.

Bruce Sherwood

unread,
Oct 26, 2021, 11:45:56 PM10/26/21
to Glowscript Users
Nice. Here's a similar version:

def selection_sort(nums):
    xdata = []
    for i in range(len(nums)):
        xdata.append(nums[i][0])
    xdata.sort()
    newdata = []
    for x in xdata:
        for i in range(len(nums)):
            if nums[i][0] == x:
                newdata.append( [ nums[i][0], nums[i][1] ]) # make copy
                nums[i][0] = None # flag as already found
    return newdata

data = [ [3,5], [2,6], [15,3], [11,2] ]
print(data)
data2 = selection_sort(data)
print(data2)

Patrick Dufour

unread,
Oct 27, 2021, 1:03:06 PM10/27/21
to Glowscript Users
That is much more efficient than what I was doing. Thanks a lot!

Bruce Sherwood

unread,
Oct 27, 2021, 1:14:46 PM10/27/21
to Glowscript Users
Presumably it would be more efficient to remove nums[i] from the nums list rather than setting  nums[i][0] = None, so that the next time through the for loop one would be searching through a shorter list. But unless you have a REALLY long list to process, presumably all these schemes are adequately fast.

Bruce

Reply all
Reply to author
Forward
0 new messages