title: Undo sort on sorted array in javascript
body: <ol>
  <li>I have an array. I sort it.</li>
  <li>I get a second array which is <em>already</em> sorted based on the first one.</li>
  </ol>
  <p>I need to reverse the sorting on the second array.</p>
  <p>For example, if the first array (unsorted) is: <code>[9, 5, 3, 0, 2]</code> then I want to to sort it, so that it becomes <code>[0, 2, 3, 5, 9]</code>.</p>
  <p>Then I receive the second array sorted based on the first one, for example <code>[&quot;home&quot;, &quot;car&quot;, &quot;train&quot;, &quot;pc&quot;, &quot;mouse&quot;]</code>. I need it to become <code>[&quot;mouse, &quot;pc&quot;, &quot;train&quot;, &quot;home&quot;, &quot;car&quot;]</code>.</p>
  <p>I can't make a copy of the array.</p>
  <hr />
  <p>I have the following code:</p>
  <pre><code>//data_r is an array with values
  
  var i = 0;
  var sort_order = new Array();
  
  data_r.sort(function (a,b) {
      var res = a[0] - b[0];
      
      sort_order[i] = res;
      i++;
      
      return res;
  });
  </code></pre>
  <p>In the end, the the <code>sort_order</code> array will contain the actions performed when we sorted items. If I want to sort a second array exactly the same way as the first then I can do the following:</p>
  <pre><code>//data_x is an array with values
  
  var i = 0;
  data_x.sort(function (a,b) {
      i++;
      return sort_order[i-1];
  });
  </code></pre>
  <p>Now the <code>data_x</code> array is sorted exactly the same way as the <code>data_r</code> array.</p>
  <p>How can I undo sort on the <code>data_r</code> array?</p>
  <p>The following code is incorrect:</p>
  <pre><code>var unsort = new Array();
  
  for(var i = 0; i &lt; data_r.length; i++)
      unsort[i] = sort_order[i]*(-1);//-1 so we perfom the oposite action
  </code></pre>