[SciPy-User] sorting an array

Martin mdekauwe@gmail....
Sat Aug 1 05:23:47 CDT 2009


Hi,

I would ideally like to sort an multidimensional array by  the first
column, but I can only manage to adjust the whole array. For
example...

>a = array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>a
array([[ 1,  0,  2,  5],
       [ 1,  5,  6,  2],
       [ 1,  2,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  3,  2,  3],
       [ 1, 11,  2,  0],
       [ 1, 10,  1,  3],
       [ 1,  9,  0,  4],
       [ 1,  8,  9,  6]])
>sort(a, axis = 0)
array([[ 1,  0,  0,  0],
       [ 1,  2,  1,  2],
       [ 1,  3,  2,  3],
       [ 1,  4,  2,  3],
       [ 1,  5,  2,  4],
       [ 1,  8,  4,  5],
       [ 1,  9,  6,  6],
       [ 1, 10,  8,  6],
       [ 1, 11,  9,  7]])

When really what i wanted was not for the 3rd and 4th columns to also
be sorted nunmerically in that way. In unix I would reformat the first
two columns in gawk i.e. gawk '{printf("%2.3d %2.3d, %d, %d\n", $1,
$2, $3, $4)}' | sort -n -k 1

Any help would be appreciated!

Thanks

Martin


More information about the SciPy-User mailing list