With numpy, you can do an indirect sort. That is, from an array like
>> a = array([ 8, 10, 5, 2, 3, 1, 6])
And then do an indirect sort like this:
>> np.argsort(a)
>> array([5, 3, 4, 2, 6, 0, 1])
This array says something like "in the 0-th position of the ordered array should be the a[5]
one of the input array, the 1-th position of the ordered array should be a[3]
" and so on. But, is there a Numpy-powered way to get something like a "should-be-here" ordering? What do I mean? With argsort, you have the index ordering that orders the input array, so that a[np.argsort(a)]
is an ordered array. But, what I need is sort of the opposite, that is, for every element of the input array, to get the position of the element on the ordered array. For the example:
>>myweirdsort(a)
>>array([5, 6, 3, 1, 2, 0, 4])
This array says something like "a[0]
goes in the 5-th position of the ordered array, a[1]
goes in the 6-th position of the ordered array" and so-on.
By the way, when I say "Numpy-powered", I mean a vectorized Numpy-ish way to do this. A non-Numpy way should be just to iterate through every element, do something like a partition of the array, and then figure out where the element ends up in the partitioned array, but it would take too long.