[Numpy-discussion] stable sort on a recarray ?
butterw@gmai...
butterw@gmai...
Tue Mar 29 13:33:12 CDT 2011
>> sortind = np.argsort(x['name'], kind='mergesort'); x[sortind]
The indirect sorting method that was suggested works for doing stable sort
on recarrays / structured arrays based on a single-column.
# It is necessary to specify kind='mergesort' because argsort is not stable:
> np.argsort(np.ones(100))
array([ 0, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57,
56, 55, 54, 53, 52, 73, 51, 74, 76, 97, 96, 95, 94, 93, 92, 91, 90,
89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 75, 50, 49, 48,
21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
4, 3, 2, 1, 22, 23, 24, 25, 47, 46, 45, 44, 43, 42, 41, 40, 39,
38, 98, 37, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 36, 99])
Any suggestions on how to achieve stable sort based on multiple columns
with numpy ?
a = array([('a', 1, 1), ('a', 0, 1), ('a', 0, 0), ('b', 0, 2)],
dtype=[('name', '|S10'), ('x', '<i4'), ('y', '<i4')])
name xy
a 1 1
a 0 1
a 0 0
b 0 2
# perform sort on primary column 'name' first, then if required on
secondary column 'x'
>>> argsort(a, order=('name', 'x')
array([2, 1, 0, 3])
# we get the expected result but the sort is not stable, desired result was:
array([2, 0, 1, 3])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110329/2b181dff/attachment.html
More information about the NumPy-Discussion
mailing list