Strange results when sorting array with fields

Travis Oliphant oliphant.travis at
Mon Oct 23 16:30:31 CDT 2006

Albert Strasheim wrote:
> Hello all
> I'm trying to sort an array with two fields, but I'm getting a result that
> doesn't seem to make sense.
> What I tried (first attempt): I have two 2-D arrays. I would like to sort
> one based on the sort of the other. I managed to do this with argsort.
> However, the fancy indexing required to get the sorted array using what
> argsort returned was very slow. I followed this example:
> b32afd722
> What I tried (second attempt): I created an array with two fields. I
> think/hope/expected that sorting the array would sort it on the first field
> in the dtype and then on the second. This is *much* faster than the fancy
> indexing approach.
That kind of sorting is what lexsort does (although with indexes) and is 
more complicated than what the sort routine does in NumPy.  The sorting 
routines in NumPy use the output of the comparison operator for the type 
to compute the result. 

An array with fields is of type void.  Right now, the VOID_compare 
routine is equivalent to the STRING_compare routine (i.e. raw bytes are 
compared).  I doubt this will do what you want in most cases.  It would 
be possible to adapt this compare routine when fields are present and so 
something like compare the first field first and the second field only 
if the first is equal.  But, this would require a bit of work and is 
probably best left for 1.0.1 or later.


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list