[Numpy-discussion] stable sort on a recarray ?
Peter Butterworth
butterw at gmail.com
Tue Mar 29 15:21:04 EDT 2011
np.lexsort does the job for both the single or multi-column stable
sort cases, thanks.
a = np.array([('a', 1, 1), ('a', 0, 1), ('a', 0, 0), ('b', 0, 2)],
dtype=[('name', '|S10'), ('x', '<i4'), ('y', '<i4')])
sortind = np.lexsort([a['x'], a['name']])
>> sortind
array([1, 2, 0, 3], dtype=int64)
>> a[sortind]
array([('a', 0, 1), ('a', 0, 0), ('a', 1, 1), ('b', 0, 2)],
dtype=[('name', '|S10'), ('x', '<i4'), ('y', '<i4')])
The documentation could perhaps benefit from some clarification:
>> http://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html#numpy.lexsort
. It is not mentioned on that page that lexsort is a stable sort.
. no structured array / recarray example is given
. it also states that "Structured arrays are sorted lexically by
argsort", but fails to mention that the resulting sort is not stable.
--
thanks,
peter butterworth
More information about the NumPy-Discussion
mailing list