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 with the Map object. Just set the key/value as index/value and use the Array.from to get the Iterator as a bi-dimensional array then sort either the indexes, the values or both.
function sorting(elements) {
const myMap = new Map();
elements.forEach((value, index) => {
myMap.set(index, value);
});
const arrayWithOrderedIndexes = Array.from(myMap.entries()).sort((left, right) => {return left[1] < right[1] ? -1 : 1});
myMap.clear();
return arrayWithOrderedIndexes.map(elem => elem[0]);
}
const elements = ['value','some value','a value','zikas value','another value','something value','xtra value'];
sorting(elements);