title: How to keep array index>value after sorting
body: <p>in javascript I have next array:</p>
  
  <blockquote>
  <pre><code>var a = [0, 2, 1, 3];
  </code></pre>
  </blockquote>
  
  <p>Where this array index>value pair is:</p>
  
  <blockquote>
    <p>0 = 0, 1 = 2, 2 = 1, 3 = 3</p>
  </blockquote>
  
  <p>What is simplest and most elegant way of keeping array index numbers after sorting an array. After sort() index>value pair should be like this:</p>
  
  <blockquote>
    <p>0 = 0, 2 = 1, 1 = 2, 3 = 3</p>
  </blockquote>
  
  <p>.. but I should be able to display those sorted values. Problem is that array cannot list by jumping index position 0, 2, 1, 3 but only as 0, 1, 2, 3.</p>
  
  <p>Can I somehow make a new array whose array values will be those new index positions, and then sort this new array but keep memorized former index>value pairs.</p>
  
  <p>Although it sound simple I cannot find solution to this.</p>
  
  <p>Thanks</p>
  
  <p>P.S. I actually want to sort by number of spaces between words in phrases contained in array. Then I want to display sorted by the number of spaces (phrases with most words first).</p>
  
  <pre><code>var input = ["zero", "here two spaces", "none", "here four spaces yes"];
  var resort = [];
  for (i = 0; i &lt; input.length; i++) {
    var spaces = (input[i].split(" ").length - 1);
    resort.push(spaces); // new array with number of spaces list
  }
  </code></pre>