I am trying to sort the array of letters by their frequency of use. but not getting the correct output. here is my try:
const ar = ['b', 'c', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'c'];
const re = ar.reduce((ar, l) => {
ar[l] ? ar[l]++ : (ar[l] = 1); //finding count of each instance
return ar;
}, {});
const vls = [];
for (const [key, val] of Object.entries(re)) {
for (let i = 0; i < val; i++) {
vls.push(key); //re creating array by thier count
}
}
const sr = vls.sort((a, i) => (a < i ? 1 : -1)); //not works
console.log(sr);
//out put should be letters frequecy ['e', 'e', 'e', 'e','a', 'a', 'a','c','c','b]
Any short and quick way to go? Thanks in advance.