Is there a way to sort the rows of a numpy ndarray using a key (or comparator) function, without resorting to converting to a python list?
In particular, I need to sort according to this function:
c1,c2= 4,7
lambda row: c1*(row[1]/c2)+row[0]
I realise one possible solution would be to generate a vector with the key value of each row, but how would one sort according to it? Should one seek to convert such vector into a index vector somehow?
order= c1*(matrix[:,1]/c2)+matrix[:,0]
indexes= order_to_index( order )
return matrix[ indexes ]
Is this realistic?
order_to_index? Do you meanargsort?np.argsort([3,6,2,2,8]) #-> array([2, 3, 0, 1, 4]). It doesn't sort your original input. Usually numpy functions don't have side effects