I did something similar a while ago with javascript but can't find it now. This is the gist of how I used two arrays to store words as they are first encountered and to increment the count for them in the second array each time the word is seen again. Finally the arrays are combined for sorting.
// words have been split from text, feed each in turn into:
if (wordArray.indexOf(currentWord) > -1)
{
// word has already been found and a count added to array;
// increment count in count array;
countArray[wordArray.indexOf(currentWord)]++;
}
else
{
// word not seen before so add to word array and initialise countArray to 1 at the same index;
wordArray[wordArray.length]=currentWord;
countArray[wordArray.length]=1;
}
// next word.
At the end you will have two arrays, one containing all the words, the other containing the count for that word at the same index. To use an array sort on this, they'd probably have to be combined into a two dimensional array:
for (i=0; i
{
combinedArray[i][0] = wordArray[i];
combinedArray[i][1] = countArray[i];
}
(I think that doing this at the end will be much faster than loading the words and counts into a two dimensional array initially as the search for words already added would be slower in a 2-d array)
Finally, sort by count using a normal sort function such as:
combinedArray.sort(sort2DimArray);
function sort2DimArray(a,b)
{
// this sorts the array using the second element
return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));
}
If the code above got clipped, I've pasted it here:
http://pastebin.com/wFv0JP7L