[Numpy-discussion] stable sort for structured dtypes?

Charles R Harris charlesr.harris at gmail.com
Fri May 11 12:00:06 EDT 2012

On Fri, May 11, 2012 at 9:01 AM, Benjamin Root <ben.root at ou.edu> wrote:

> Hello all,
> I need to sort a structured array in a stable manner.  I am also sorting
> only by one of the keys, so I don't think lexsort() is stable in that
> respect.  np.sort() allows for choosing 'mergesort', but it appears to not
> be implemented for structured arrays.  Am I going to have to create a new
> plain array out of the one column I want to sort by, and run np.artsort()
> with the mergesort in order to get around this?  Or is there something more
> straightforward that I am missing?

Lexsort is just a sequence of indirect merge sorts, so using it to sort on
a single column is the same as calling argsort(..., kind='mergesort').

Mergesort (and heapsort) need to be extended to object arrays and arrays
with specified comparison functions. I think that would be an interesting
project for someone, I've been intending to do it myself but haven't got
around to it.

But as to your current problem, you probably need to have the keys in a
plain old array. They also need to be in a contiguous array, but the sort
methods take care of that by making contiguous copies when needed. Adding a
step parameter to the sorts is another small project for someone. There is
an interesting trade off there involving cache vs copy time vs memory usage.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120511/714887e1/attachment.html>

More information about the NumPy-Discussion mailing list