title: c++ method to order a matrix by elements in a column (the same of sortrows in MATLAB)
body: <p>I'm looking for a c++ method that is able to order the rows of a matrix by a decreasing order of the values in a specific column. </p>
  
  <p>For example, if this is my input: </p>
  
  <pre><code>matrix = [1,2,3;
            2,4,1;
            0,5,2]
  </code></pre>
  
  <p>the call of the method(matrix,3) should provide the following output:</p>
  
  <pre><code>outputMatrix = [1,2,3;
                  0,5,2;
                  2,4,1]
  </code></pre>
  
  <p>In MATLAB this can be done by the calling of the function:</p>
  
  <pre><code>outputMatrix = sortrows(matrix,3).
  </code></pre>
  
  <p>What about c++? (In both cases, 3 is the index of the column).</p>
  
  <p>Moreover in the script I'm working, the matrix is defined as a vector of vector: <code>std::vector&lt;std::vector&lt;double&gt; &gt; matrix</code></p>
  
  <p><strong>[EDIT]</strong> I add another example:</p>
  
  <pre><code>input = [4,5,6;
           0,2,8;
           1,2,3;
           6,7,9]
  </code></pre>
  
  <p>outputMatrix = sortrows(input,2);
  Column 2, ordered, is: 9,8,6,3; so I have to order the rows and copy the elements of the first two columns (respectively 0 and 1). </p>
  
  <pre><code> outputMatrix = [6,7,9;
                   0,2,8;
                   4,5,6;
                   1,2,3]
  </code></pre>
  
  <p>I report here the method I wrote to do this, but I don't know if this is a fast way:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;double&gt; &gt; sortrows(std::vector&lt;std::vector&lt;double&gt; &gt; matrix,int col){
      int length = matrix[col].size();
      std::vector&lt;std::vector&lt;double&gt; &gt; output(3,std::vector&lt;double&gt;(length*length));
      output[col] = matrix[col];
      std::sort(output[col].begin(),output[col].end(),std::greater&lt;double&gt;());
      for (int i = 0; i &lt; length*length;i++){
          int index = 0;
          while(output[col][i]!=matrix[col][index]){index++;}
          output[0][i]=matrix[0][index];
          output[1][i]=matrix[1][index];
          matrix[2][index] = -1;
      }
      return output;
  }
  </code></pre>