You can do this with numpy.lexsort
In [1]: import numpy as np
In [2]: a = np.array([(4,0), (1,9), (1,0), (4,9)],
dtype=[('x',int),('y',float)])
In [3]: a
Out[3]:
array([(4, 0.0), (1, 9.0), (1, 0.0), (4, 9.0)],
dtype=[('x', '<i8'), ('y', '<f8')])
In [4]: a['x']
Out[4]: array([4, 1, 1, 4])
In [5]: a['y']
Out[5]: array([ 0., 9., 0., 9.])
The order priority of the arguments to lexsort
are opposite that of np.sort(..., order=...)
. So, to sort first by descending 'x'
and then by ascending 'y'
:
In [6]: a[np.lexsort((a['y'], -a['x']))]
Out[6]:
array([(4, 0.0), (4, 9.0), (1, 0.0), (1, 9.0)],
dtype=[('x', '<i8'), ('y', '<f8')])
Notes:
- This works assuming all your values are numerical (since the negative won't reverse string sorting).
- I've seen somewhere the use of
a['x'][::-1]
as a key instead of-a['x']
but that's not working for me right now.