<div dir="ltr">Similar to Karin's but use 'dim_pqsort_n'<br> <a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_pqsort_n.shtml">http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_pqsort_n.shtml</a><br><br><br> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)<br><br> i = dim_pqsort_n(Ain(:,0), 1, 0)<br> print(i)<br><br> dimA = dimsizes(Ain)<br> nrow = dimA(0)<br><br> Aout = Ain<br> do nr= 0,nrow-1<br> Aout(nr,:) = Ain(i(nr),:)<br> end do<br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 6, 2016 at 3:30 AM, Karin Meier-Fleischer <span dir="ltr"><<a href="mailto:meier-fleischer@dkrz.de" target="_blank">meier-fleischer@dkrz.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Hi Abhik,<br>
<br>
you can use NCLs function <i>dim_pqsort </i>to sort the array by
the first column.<br>
<br>
<font color="#3333ff"><tt>begin</tt><span class=""><tt><br>
</tt><tt> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)</tt><tt><br>
</tt><tt><br>
</tt></span><tt>;-- criteria is to sort the array Ain by increasing of
the first column</tt><tt><br>
</tt><tt> cols_0 = Ain(:,0) <wbr> ;-- get first
columns of each row</tt><tt><br>
</tt><tt> i0 = dim_pqsort(cols_0,2) <wbr> ;-- sorting
by increasing and overwrite cols_0</tt><tt> return sorted
indices i0</tt><tt><br>
</tt><tt><br>
;-- resort Ain</tt><tt><br>
</tt><tt> Aout = Ain(i0,:) <wbr> ;-- use
sorted indices of first column to sort the complete array</tt><tt><br>
</tt><tt><br>
</tt><tt>;-- print the new array in a nicer way</tt><tt><br>
</tt><tt> opt = True</tt><tt><br>
</tt><tt> opt@title = "Sort array by first column of each row"</tt><tt><br>
</tt><tt> write_matrix(Aout,"3I4",opt)</tt><tt><br>
</tt><tt><br>
</tt><tt>end</tt></font><br>
<br>
This will print Aout<br>
<br>
<font color="#3333ff"><tt> Sort array by first column of each row</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> 1 7 4</tt><tt><br>
</tt><tt> 2 1 3</tt><tt><br>
</tt><tt> 3 1 5</tt><tt><br>
</tt><tt> 6 2 4</tt></font><br>
<br>
Bye,<br>
Karin<br>
<br>
<div class="m_-552006274047022034moz-cite-prefix">Am 06.12.16 um 06:59 schrieb Abhik
Santra:<br>
</div><div><div class="h5">
<blockquote type="cite">
<pre>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
******************************<wbr>**********
Abhik Santra
Postdoctoral Research Fellow
Australian Bureau of Meteorology,
Melbourne, Australia
******************************<wbr>**********
______________________________<wbr>_________________
ncl-talk mailing list
<a class="m_-552006274047022034moz-txt-link-abbreviated" href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a>
List instructions, subscriber options, unsubscribe:
<a class="m_-552006274047022034moz-txt-link-freetext" href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/<wbr>mailman/listinfo/ncl-talk</a>
</pre>
</blockquote>
<br>
</div></div></div>
<br>______________________________<wbr>_________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/<wbr>mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>