Suppose I have a Javascript array, like so:
var test = [\'b\', \'c\', \'d\', \'a\'];
I want to sort the array. Obviously, I can just do th
Dave Aaron Smith is correct, however I think it is interesting to use Array map() here.
var test = ['b', 'c', 'd', 'a'];
// make list with indices and values
indexedTest = test.map(function(e,i){return {ind: i, val: e}});
// sort index/value couples, based on values
indexedTest.sort(function(x, y){return x.val > y.val ? 1 : x.val == y.val ? 0 : -1});
// make list keeping only indices
indices = indexedTest.map(function(e){return e.ind});