concept sort in category functional programming

This is an excerpt from Manning's book Grokking Functional Programming.
Comparator<String> scoreComparator =new Comparator<String>() {public int compare(String w1, String w2) {return Integer.compare(score(w2), score(w1));}}; scoreComparatorcompares two strings and prefers the one that has higher
scoreNow, given a list of words we can get the results by using the sort method that takes the Comparator we created:
↑
static List<String> rankedWords(List<String> words) {words.sort(scoreComparator);return words;}This box means that the value in the text will be used later in the chapter.
Now let’s test the rankedWords function we’ve just created:
List<String> words =Arrays.asList(“ada”, “haskell”, “scala”, “java”, “rust”);List<String> ranking = rankedWords(words);System.out.println(ranking);[haskell, rust, scala, java, ada]Note that the score function counts the letters that are different than ‘a’. That’s why "haskell" scored 6,
It worked! haskell is at the top of the ranking, because its score is 6. There is one problem though. We mutate the existing words list inside the function! Note that sort method returns void making it obvious that it mutates the argument that we passed. This solution violates the rules of a pure function and invites all the problems we’ve discussed in previous chapters. We can do better, and do it in Java!
↲
↰
Please forgive me if you experienced chapter 2 flashbacks while reading the code that mutates existing values.
It worked! haskell is at the top of the ranking, because its score is 6. There is one problem though. We mutate the existing words list inside the function! Note that sort method returns void making it obvious that it mutates the argument that we passed. This solution violates the rules of a pure function and invites all the problems we’ve discussed in previous chapters. We can do better, and do it in Java!