What is the best method to sort a sparse array and keep the elements on the same indexes? For example:
a[0] = 3,
a[1] = 2,
a[2] = 6,
a[7] = 4,
a[8] = 5,
You can
a
.var b = a.filter(function(x) {
return true;
}).sort(function(x,y) {
return y - x;
});
a = a.map([].pop, b);
Or, in ECMAScript 2017,
a = a.map([].pop, Object.values(a).sort((x,y) => y-x));