title: Javascript: Sort array and return an array of indices that indicates the position of the sorted elements with respect to the original elements
body: <p>Suppose I have a Javascript array, like so: </p>
  
  <pre><code>var test = ['b', 'c', 'd', 'a'];
  </code></pre>
  
  <p>I want to sort the array. Obviously, I can just do this to sort the array:</p>
  
  <pre><code>test.sort(); //Now test is ['a', 'b', 'c', 'd']
  </code></pre>
  
  <p>But what I really want is an array of indices that indicates the position of the sorted elements with respect to the original elements. I'm not quite sure how to phrase this, so maybe that is why I am having trouble figuring out how to do it. </p>
  
  <p>If such a method was called sortIndices(), then what I would want is:</p>
  
  <pre><code>var indices = test.sortIndices();
  //At this point, I want indices to be [3, 0, 1, 2].
  </code></pre>
  
  <p>'a' was at position 3, 'b' was at 0, 'c' was at 1 and 'd' was a 2 in the original array. Hence, [3, 0, 1, 2].</p>
  
  <p>One solution would be to sort a copy of the array, and then cycle through the sorted array and find the position of each element in the original array. But, that feels clunky. </p>
  
  <p>Is there an existing method that does what I want? If not, how would you go about writing a method that does this?</p>