I am trying to sort an array like below arrange according to frequency, but it seems to be according to alphabetical order now;
https://jsbin.com/kicusixoha/edit?html,js,console,output
const arr = ["apples", "oranges", "oranges", "oranges",
"bananas", "bananas", "oranges"
];
let counter = arr.reduce(
(counter, key) => {
counter[key] = 1 + counter[key] || 1;
return counter
}, {});
console.log(counter)
The output above is:
{
apples: 1,
bananas: 2,
oranges: 4
}
but I need it to be like below with the most frequency to be the first:
{
oranges: 4,
bananas: 2,
apples: 1,
}
Any help would be greatly appreciated.