Within a piece of code I have a numpy array already constructed, and I want to sort this 1st array given a specific order specified in a list. The result will be a 3rd array (new_values).
The 1st array has the values to be sorted.
values = numpy.array([10.0, 30.1, 50, 40, 20])
The list provides the order given by the indices of the values in new_values which should be in descending order where 0 corresponds to the largest number.
order=[0, 3, 1, 4, 2].
So,
new_values[0] > new_values[3] > new_values[1] > new_values[4] > new_values[2]
I tried to find a specific sort function to accomplish this such as argsort or with a key, however I did not understand how to adapt these to this situation.
Is there a simple quick method to accomplish this as I will be doing this for many iterations. I am willing to change the 2nd array, order, to specify the indices in another way if this was advantageous to sorting with a better method.
Currently I am using the following for loop.
size=len(values)
new_values = np.zeros(size)
values = sorted(values,reverse=True)
for val in range(0, size):
new_values[int(order[val])] = values[val]
Thank you in advance!