I can do an in-place reverse sort (descending sort) of a numpy array, but I also need to be able to unsort (undo) it later.
Given an unsorted example:
a = np.array([-1, -2, 1, -3, 2, 0])
I tried:
i = a[::-1].argsort().argsort() # BAD attempt to store original index
# i = array([3, 5, 0, 4, 1, 2])
a[::-1].sort() # in-place reverse sort (works correctly)
# a= array([ 2, 1, 0, -1, -2, -3])
a = a[i] # FAILS to restore original a
# a = array([-1, -3, 2, -2, 1, 0])
The above doesn't work. What would be the correct i
that would work? Assume the array is very large, and so we don't want to make any unnecessary copies.