Sort Words By Score [closed]

I need to sort word suggestions by descending score using the CompareTo method, but I’m not sure how to go about it. Here’s a sample of expected output using a .txt file containing a few words. The number to the right of each suggestion is the score.

Enter a letter or letters for suggestions:
You entered: cat
0. cat: 3
1. cats: 2
2. catty: 1

Enter a letter or letters for suggestions:
You entered: cat
0. cat: 4
1. cats: 3
2. catty: 2

And so on.

Answer

Score is already an int so it doesn’t need parsing. You can use the Comparator supplied with the Integer class to do the comparison. If you’re reading these from a file, you will need to convert the score to an int before storing in the field.

@Override
public int compareTo(Words w) {
       return Integer.compare(this.score, w.score);
}