Tom,<br><br>The list -- nee tuple, thanks Travis -- is the list of key sequences and each key sequence can be a column in a matrix. So for instance if you wanted to sort on a few columns of a matrix, say columns 2,1, and 0, in that order, and then rearrange the rows so the columns were ordered, you would do something like:
<br><br><span style="font-family: courier new,monospace;">>>> a = randint(0,2,(7,4))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">>>> a</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">array([[0, 0, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [0, 0, 1, 0],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> [1, 0, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [0, 1, 0, 1],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> [1, 1, 1, 0],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [0, 1, 1, 1],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> [0, 1, 0, 1]])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">>>> ind = lexsort((a[:,2],a[:,1],a[:,0]))
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">>>> sorted = a[ind]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
>>> sorted</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">array([[0, 0, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
[0, 0, 1, 0],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [0, 1, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
[0, 1, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [0, 1, 1, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
[1, 0, 0, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [1, 1, 1, 0]])</span><br style="font-family: courier new,monospace;"><br>Note that the last key defines the major order.
<br><br>Chuck<br><br><div><span class="gmail_quote">On 6/1/06, <b class="gmail_sendername">Tom Denniston</b> <<a href="mailto:tom.denniston@alum.dartmouth.org">tom.denniston@alum.dartmouth.org</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;">
This function is really useful but it seems to only take tuples not<br>ndarrays. This seems kinda strange. Does one have to convert the<br>ndarray into a tuple to use it? This seems extremely inefficient. Is<br>there an efficient way to argsort a 2d array based upon multiple
<br>columns if lexsort is not the correct way to do this? The only way I<br>have found to do this is to construct a list of tuples and sort them<br>using python's list sort. This is inefficient and convoluted so I was<br>
hoping lexsort would provide a simple solution.<br><br>--Tom<br><br><br>_______________________________________________<br>Numpy-discussion mailing list<br><a href="mailto:Numpy-discussion@lists.sourceforge.net">Numpy-discussion@lists.sourceforge.net
</a><br><a href="https://lists.sourceforge.net/lists/listinfo/numpy-discussion">https://lists.sourceforge.net/lists/listinfo/numpy-discussion</a><br></blockquote></div><br>