On Mon, Mar 28, 2011 at 3:34 PM, <[email protected]> wrote:
> What is the recommended way to perform a stable sort on a recarray /
> structured array ?
>
> In the following example, I want to sort on name, while retaining the
> existing order in the case of equal values:
> values = [('a', 1), ('a', 0), ('b', 2)]
> dtype = [('name', 'S10'), ('val', 'i4')]
>>>> a = np.array(values, dtype=dtype)
> array([('a', 1), ('a', 0), ('b', 2)],
> dtype=[('name', '|S10'), ('val', '<i4')])
>>>> np.sort(a, order='name')
> array([('a', 0), ('a', 1), ('b', 2)],
> dtype=[('name', '|S10'), ('val', '<i4')])
I don't think it's directly possible since each row/record of a
structured array is treated as one matrix element.
argsort looks stable, but I don't know if this is guaranteed.
>>> np.argsort(np.ones(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.argsort(np.r_[2*np.ones(5),np.ones(5)])
array([5, 6, 7, 8, 9, 0, 1, 2, 3, 4])
according to the docs only mergesort is stable,
so sorting only on one column should work, something like this might work
x = array([('a', 0), ('a', 1), ('b', 2)], dtype=[('name', '|S10'),
('val', '<i4')])
sortind = np.argsort(x]'name']) #uses quicksort, not stable?
or
sortind = np.argsort(x]'name'], kind='mergesort')
x[sortind]
Josef
> _______________________________________________
> NumPy-Discussion mailing list
> [email protected]
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion