I have an XMLList 'Keywords', which consists of about 30 elements. I want to count the number of unique keywords in the List, and how often they occur. Then display the top 3 most occuring keywords.
There's probably a simple sorting/count function to carry this out, however, i'm quite new to as3 so please forgive my naivety.
Cheers.
I don't think there's a one-line fix for this.
The most straightforward way I can see of doing this is as follows:
Put your keywords into an array so you can sort them. This results in multiple occurrances of any keyword being grouped together.
Now make a second array to hold a key-value pair for each keyword (the value is the number of occurrances). You can do this by stepping through the first array and examining each value: either it's the start of a group (so add a new k-v object to the second array), or it's another occurrance of the previous keyword (so increment the count value of the last k-v object).
Now you can use the sortOn() method on your second array to sort by the number of occurrances of each keyword.
The code (this may need some fixing - afraid my AS3 is not so good yet, and I'm not able to test it... writing this on my iPhone!) would look something like this:
Hope this works for you.