[Numpy-discussion] stable sort for structured dtypes?

Benjamin Root ben.root at ou.edu
Fri May 11 12:36:42 EDT 2012


On Fri, May 11, 2012 at 12:00 PM, Charles R Harris <
charlesr.harris at gmail.com> wrote:

>
>
> 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.
>
> Chuck
>
>
Ok, that clears it up for me.  I ended up just doing an
argsort(np.array(d['vtime']), kind=...) and use the indices as a guide.  My
purpose didn't require a resorted array anyway, so this will do for now.

Thanks,
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120511/2ab5f465/attachment.html>


More information about the NumPy-Discussion mailing list