Added a function to sort words by frequencies