Strange results when sorting array with fields

Albert Strasheim fullung at gmail.com
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:

http://www.scipy.org/Numpy_Example_List#head-9f8656795227e3c43e849c6c0435eee
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.

Code:

import numpy as N
print N.__version__
print
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
print
x.sort(kind='quicksort') # other kinds not supported
print 'after sort:'
print x

Output on my system:

1.0.dev3376

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?

Thanks!

Regards,

Albert


-------------------------------------------------------------------------
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
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list