fix implementation of sort_counted_words