Sorting Multidimesional array(newbie)

Travis E. Oliphant oliphant.travis at ieee.org
Tue Dec 12 13:50:47 EST 2006


Tartifola wrote:
> Hi,
> how can I sort an array like
> 
> array([[5, 2],
>        [1, 3]])
> 
> along the first column to obtain
> 
> array([[1, 3],
>        [5, 2]])
> i.e. keeping track of the ordered couples?
> 
> Thanks,
> A

Just to add one more solution to this question that works with numpy.

You can also view the array using fields and then sort (which will do 
lexicographic ordering) and convert the result back to the original view.

Something like this:

a = array([[5,2],[1,3]])
dt = a.dtype
g = a.view([('',dt),('',dt)])
g.sort(0)
a = g.view(dt)


-Travis








More information about the Python-list mailing list