title: sorting values in vector matrix, output corresponding indices
body: <p>I found this question below which got some good answers:</p>
  
  <p><a href="https://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes/12399290#12399290">For example, I have a set, or vector, or matrix of samples A : [5, 2, 1, 4, 3]. I want to sort these to be  B : [1,2,3,4,5], but I also want to remember the original indexes of the values, so I can get another set which would be:  C : [2, 1, 4, 3, 0 ] - which corresponds to the index of the each element in 'B', in the original 'A'.</a></p>
  
  <p>what if i extend the problem to 2 indices, say for example a 2x3 matrix</p>
  
  <p>1 5 6</p>
  
  <p>4 8 2</p>
  
  <p>sorted (in descending, neither columnwise/rowwise, by values only)</p>
  
  <p>8 6 5 4 2 1</p>
  
  <p>output indices</p>
  
  <p>(1,1) (0,2) (0,1) (1,0) (1,2) (0,0)</p>
  
  <p>i tried but im sure i needed help and any help would mean so much...</p>
  
  <p><code>template &lt;typename T&gt;
  vector &lt;vector &lt;size_t&gt;&gt; sort_indexes(const vector &lt;vector &lt;T&gt;&gt; &amp;v)</code></p>
  
  <pre><code>//// initialize original index locations
  vector&lt;vector&lt;size_t&gt;&gt; idx(v.size());
  for (size_t i = 0; i != idx.size(); ++i) {
      idx[i] = i;
      for (size_t j = 0; i != idx[i].size(); ++j) {
          idx[j] = j;
      }
  }
  // sort indexes based on comparing values in v
  sort(idx.begin(), idx.end(),
      [&amp;v](size_t i1, size_t i2) {return v[i1] &gt; v[i2]; });
  
  return idx;
  </code></pre>
  
  <p>}</p>
  
  <p>int main (){</p>
  
  <pre><code>vector&lt;vector&lt;int&gt;&gt; v;
  v[0].push_back(10);
  v[0].push_back(1);
  v[0].push_back(3);
  v[1].push_back(20);
  v[1].push_back(30);
  v[1].push_back(40);
  
  for (auto i = 0; i &lt; v.size(); i++) {
      for (auto j:sort_indexes(v) )
      cout &lt;&lt; i &lt;&lt; j &lt;&lt;"  "&lt;&lt; v[i][j] &lt;&lt; endl;
  }
  cin.get();
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>i adopted this <a href="https://stackoverflow.com/a/12399290/5807825">https://stackoverflow.com/a/12399290/5807825</a> for my problem.i tried some tweaks on the code, can anyone shed some light please which parts are wrong, or everything...i hope not.</p>