<br><br><div><span class="gmail_quote">On 10/22/06, <b class="gmail_sendername">Albert Strasheim</b> <<a href="mailto:fullung@gmail.com">fullung@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello all<br><br>I'm trying to sort an array with two fields, but I'm getting a result that<br>doesn't seem to make sense.<br><br>What I tried (first attempt): I have two 2-D arrays. I would like to sort<br>one based on the sort of the other. I managed to do this with argsort.
<br>However, the fancy indexing required to get the sorted array using what<br>argsort returned was very slow. I followed this example:</blockquote><div><br>It is certainly awkward. I am going to add a function to do this after 
1.0 comes out. Sounds like you need it now.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><a href="http://www.scipy.org/Numpy_Example_List#head-9f8656795227e3c43e849c6c0435eee">
http://www.scipy.org/Numpy_Example_List#head-9f8656795227e3c43e849c6c0435eee</a><br>b32afd722<br><br>What I tried (second attempt): I created an array with two fields. I<br>think/hope/expected that sorting the array would sort it on the first field
<br>in the dtype and then on the second. This is *much* faster than the fancy<br>indexing approach.</blockquote><div><br>I believe it sorts on the two fields together as one big binary blob.<br></div><snip><br><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Output on my system:<br><br>1.0.dev3376<br><br>before sort:<br>[[(1.0, 1) (2.0, 2)]<br> [(3.0, 3) (4.0, 4)]]<br><br>after sort:<br>[[(2.0, 2) (1.0, 1)]<br> [(4.0, 4) (3.0, 3)]]<br><br>The already sorted array has been unsorted in some way. Any thoughts?
</blockquote><div><br>I suspect something to do with the binary representation of the floats. Probably depends on big/little endian also.  The floats 1.0, 2.0, and 4.0 all have zero mantissas while 3.0 has a one. The sort behaves differently if uint8 is used for both fields.
<br><br></div><span style="font-family: courier new,monospace;">In [3]: dt = dtype([('aaa', uint8), ('bbb', uint8)])</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [4]: x = empty((2, 2), dt)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [5]: xa = x[dt.names[0]]</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [6]: xa[:] = [[1,2], [3,4]]</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [7]: xb = x[dt.names[1]]</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [8]: xb[:] = [[1,2], [3,4]]</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [9]: x.sort()</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [11]: x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Out[11]: </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">array([[(1, 1), (2, 2)],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">       [(3, 3), (4, 4)]], </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">      dtype=[('aaa', '|u1'), ('bbb', '|u1')])</span><br> 
<br><div><br>Chuck <br></div><br></div><br>