<div dir="ltr"><div>Actually, the following is more &#39;elegant&#39;<br><br><span class="gmail-im">Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)<br><br></span> i    = dim_pqsort_n(Ain(:,0), 1, 0)<br></div> Aout = Ain(i,:)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 6, 2016 at 6:20 AM, Dennis Shea <span dir="ltr">&lt;<a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Similar to Karin&#39;s but use &#39;dim_pqsort_n&#39;<br>  <a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_pqsort_n.shtml" target="_blank">http://www.ncl.ucar.edu/<wbr>Document/Functions/Built-in/<wbr>dim_pqsort_n.shtml</a><span class=""><br><br><br> Ain = (/ (/2,1,3/), (/6,2,4/), (/3,1,5/), (/1,7,4/) /)<br><br></span> 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="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 6, 2016 at 3:30 AM, Karin Meier-Fleischer <span dir="ltr">&lt;<a href="mailto:meier-fleischer@dkrz.de" target="_blank">meier-fleischer@dkrz.de</a>&gt;</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><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 = &quot;Sort array by first column of each row&quot;</tt><tt><br>
      </tt><tt>  write_matrix(Aout,&quot;3I4&quot;,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_3092655533185321211m_-552006274047022034moz-cite-prefix">Am 06.12.16 um 06:59 schrieb Abhik
      Santra:<br>
    </div><div><div class="m_3092655533185321211h5">
    <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_3092655533185321211m_-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_3092655533185321211m_-552006274047022034moz-txt-link-freetext" href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/mailma<wbr>n/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" target="_blank">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/mailma<wbr>n/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>