Strange results when sorting array with fields

Albert Strasheim fullung at
Sun Oct 22 19:47:10 CDT 2006

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:

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.


import numpy as N
print N.__version__
dt = N.dtype([('aaa', N.float64), ('bbb', N.uint8)])
x = N.empty((2, 2), dt)
xa = x[dt.names[0]]
xa[:] = [[1.,2], [3.,4.]]
xb = x[dt.names[1]]
xb[:] = [[1, 2], [3, 4]]
print 'before sort:'
print x
x.sort(kind='quicksort') # other kinds not supported
print 'after sort:'
print x

Output on my system:


before sort:
[[(1.0, 1) (2.0, 2)]
 [(3.0, 3) (4.0, 4)]]

after sort:
[[(2.0, 2) (1.0, 1)]
 [(4.0, 4) (3.0, 3)]]

The already sorted array has been unsorted in some way. Any thoughts?




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