const myDict = { // helper object to assign numeric values for High, Average, Low
'HIGH': 2, 'AVERAGE': 1, 'LOW': 0
};
const customSort = arr => (
[...arr].sort((a, b) => ( // shallow-copy 'arr' and use '.sort()'
a in myDict && b in myDict // if both a & b are High, Average or Low
? myDict[b] - myDict[a] // find difference in their costs (using 'myDict')
: b in myDict // else, if b is either High, Average or Low
? -1 // then, keep b below a
: a in myDict // else, if a is either High, Average or Low
? 1 // then, keep a below b
: +b - +a // else, if both a & b are numeric-strings
)) // convert to number (using '+') and order based on difference
);
const rawData = ['1', '99', '10', 'HIGH', '5', 'LOW'];
console.log(`[${rawData.join(', ')}]`, customSort(rawData));
console.log(
"['15', 'AVERAGE', '20', 'HIGH', '52', 'LOW']",
customSort(['15', 'AVERAGE', '20', 'HIGH', '52', 'LOW'])
);
console.log(
"['31', '49', '17', 'AVERAGE', '25', 'LOW']",
customSort(['31', '49', '17', 'AVERAGE', '25', 'LOW'])
);
console.log(
"['41', 'HIGH', '90', 'LOW', '45', 'AVERAGE']",
customSort(['41', 'HIGH', '90', 'LOW', '45', 'AVERAGE'])
);
console.log(
"['71', '32', '05', 'HIGH', '75', 'LOW']",
customSort(['71', '32', '05', 'HIGH', '75', 'LOW'])
);