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
you can do this !
detailItems.slice()
.map((r, ix) => {
r._ix = ix;
return r;
})
.sort((a,b) => {
... /* you have a._ix or b._ix here !! */
})
.slice()
clones your array to prevent side effects :))
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});