# [Numpy-discussion] argsort

Wed Jan 16 02:30:55 CST 2013

```Hi,

Thanks everybody for all the answers that make perfect sense when axis=0.

Now suppose I want to sort the array in such a way that each row is
sorted individually. Then I suppose I should do this:

from numpy import *

v = array([[4,3],
[1,12],
[23,7],
[11,6],
[8,9]])
idx = argsort(v, axis=1)

idx is then

[[1 0]
[0 1]
[1 0]
[1 0]
[0 1]]

which makes sense, since these are the indices in an order that would
sort each row. But when I try

I just get strange results. Anybody that can point me towards the
correct solution.

Best regards,

On 01/15/2013 09:53 PM, eat wrote:
> Hi,
>
> On Tue, Jan 15, 2013 at 1:50 PM, Mads Ipsen <madsipsen@gmail.com
>
>     Hi,
>
>     I simply can't understand this. I'm trying to use argsort to
>     produce indices that can be used to sort an array:
>
>       from numpy import *
>
>       indices = array([[4,3],[1,12],[23,7],[11,6],[8,9]])
>       args = argsort(indices, axis=0)
>       print indices[args]
>
>     gives:
>
>     [[[ 1 12]
>       [ 4  3]]
>
>      [[ 4  3]
>       [11  6]]
>
>      [[ 8  9]
>       [23  7]]
>
>      [[11  6]
>       [ 8  9]]
>
>      [[23  7]
>       [ 1 12]]]
>
>     I thought this should produce a sorted version of the indices array.
>
>     Any help is appreciated.
>
> Perhaps these three different point of views will help you a little
> bit more to move on:
> In []: x
> Out[]:
> array([[ 4,  3],
>        [ 1, 12],
>        [23,  7],
>        [11,  6],
>        [ 8,  9]])
> In []: ind= x.argsort(axis= 0)
> In []: ind
> Out[]:
> array([[1, 0],
>        [0, 3],
>        [4, 2],
>        [3, 4],
>        [2, 1]])
>
> In []: x[ind[:, 0]]
> Out[]:
> array([[ 1, 12],
>        [ 4,  3],
>        [ 8,  9],
>        [11,  6],
>        [23,  7]])
>
> In []: x[ind[:, 1]]
> Out[]:
> array([[ 4,  3],
>        [11,  6],
>        [23,  7],
>        [ 8,  9],
>        [ 1, 12]])
>
> In []: x[ind, [0, 1]]
> Out[]:
> array([[ 1,  3],
>        [ 4,  6],
>        [ 8,  7],
>        [11,  9],
>        [23, 12]])
> -eat
>
>
>     Best regards,
>
>
>     --
>     +-----------------------------------------------------+
>     | Mads Ipsen                                          |
>     +----------------------+------------------------------+
>     | Gåsebæksvej 7, 4. tv |                              |
>     | DK-2500 Valby        | phone:+45-29716388  <tel:%2B45-29716388>  |
>     | Denmark              | email:mads.ipsen@gmail.com  <mailto:mads.ipsen@gmail.com>  |
>     +----------------------+------------------------------+
>
>
>     _______________________________________________
>     NumPy-Discussion mailing list
>     NumPy-Discussion@scipy.org <mailto:NumPy-Discussion@scipy.org>
>     http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

--
+-----------------------------------------------------+
| Mads Ipsen                                          |
+----------------------+------------------------------+
| Gåsebæksvej 7, 4. tv |                              |
| DK-2500 Valby        | phone:          +45-29716388 |
| Denmark              | email:  mads.ipsen@gmail.com |
+----------------------+------------------------------+

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130116/19346851/attachment.html
```