So I've created this JS code to get a map where every word in a string is the key and the amount of times that word appears is the value.
var txtArray = txtVar.split(" ");
var txtMap = {};
for (var i = 0; i<txtArray.length;i++){
if(txtArray[i] in txtMap)
txtMap[txtArray[i]] += 1;
else
txtMap[txtArray[i]] = 1;
}
Now i need to sort them so that I can somehow rank them by the 10 longest words and 10 most used words and so on and I guess I could find a way to do it by making a for loop where i stored them in arrays and then pushed the oldest "top" word up or down the list when a new one comes along, but I think there must be a less awkward way to do this task?
Object.keys(txtMap).sort((a, b) => txtMap[a] < txtMap[b] ? -1 : txtMap[a] > txtMap[b] ? +1 : 0).txtMap[txtArray[i]] += 1may have unexpected results in apparently random and difficult to find cases.