# [ncl-talk] Sorting an array depending on first column

Karin Meier-Fleischer meier-fleischer at dkrz.de
Tue Dec 6 03:30:03 MST 2016

```Hi Abhik,

you can use NCLs function /dim_pqsort /to sort the array by the first
column.

begin
Ain  = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)

;-- criteria is to sort the array Ain by increasing of the first column
cols_0 =  Ain(:,0)                        ;-- get first columns of
each row
i0     =  dim_pqsort(cols_0,2)            ;-- sorting by increasing
and overwrite cols_0return sorted indices i0

;-- resort Ain
Aout   =  Ain(i0,:)                       ;-- use sorted indices of
first column to sort the complete array

;-- print the new array in a nicer way
opt       =  True
opt at title = "Sort array by first column of each row"
write_matrix(Aout,"3I4",opt)

end

This will print Aout

Sort array by first column of each row

1   7   4
2   1   3
3   1   5
6   2   4

Bye,
Karin

Am 06.12.16 um 06:59 schrieb Abhik Santra:
> Hi NCL users
>
> How can I sort an array based on its first column elements?
> For example, if
>
> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)
>
> then how can I get
>
> Aout = (/ (/1,7,4/), (/2,1,3/), (/3,1,5/), (/6,2,4/) /) ?
>
> Any help would be greatly appreciated.
>
> Thanks,
> Abhik
>
> ****************************************
> Abhik Santra
> Postdoctoral Research Fellow
> Australian Bureau of Meteorology,
> Melbourne, Australia
> ****************************************
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-------------- next part --------------
An HTML attachment was scrubbed...