-2

I have been working on a Scrabble assignment. I need to read words from a list, then read each char and assign a value, eventually assigning a total score to each word. That has been done! Phew. Now I need to use the Comparator to sort the words from greatest score to least. I have done a lot of reading and I'm still confused. I know that I could use the interface, but there's also using Comparator with a lambda expression, which is the direction that I think I want to go. I'm just not sure how. I need to compare the sumValue I have for each word, then print the words in decreasing order.

I created 2 loops to read the word (i), then the chars (j). I have printed to the screen the score of each word (sumValue) and its location (i) in my ArrayList. Now I need to use Comparator to compare the scores, and then reorder the location. I think my problem is that I feel like I'm not sorting the Arraylist. I'm sorting the scores, which are not in an ArrayList. Do I need to create a new ArrayList with scores attached to each word and sort that?

ScarletCas
  • 21
  • 5
  • What programming language are your working with? – joanis Nov 06 '22 at 15:34
  • Could you share an example – Kuncheria Nov 06 '22 at 15:35
  • If you're working in Java, this might help: https://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort – joanis Nov 06 '22 at 15:37
  • I'm working in Java. I added a couple of pics of what I have so far, with the middle cut out as all the if statements don't need to be shown. I am a beginning student. My code is cumbersome, I know. But I'm pretty proud of what I was able to do on my own. Just need guidance on how to use Comparator correctly. – ScarletCas Nov 06 '22 at 15:49
  • 3
    [Don't post images of code](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – Silvio Mayolo Nov 06 '22 at 16:13
  • 2
    Please post the actual code and not images. – WJS Nov 06 '22 at 16:40
  • I have removed the pictures. I apologize. I am new to posting here and didn't realize this was an issue. I don't want to copy my code as I am in a programming class and working on assignments. I am trying to be vague enough that I get pointed in the right direction without anyone giving me coded answers as that is not what I'm looking for. I will try to figure out better ways to ask my questions. – ScarletCas Nov 07 '22 at 01:04

2 Answers2

1

You can do it like so. The List interface has a sort() method that takes a Comparator.

The common practice for defining a Comparator is to make use of one of its static methods, like Comparator.comparing(Function<? super T,? extends U> keyExtractor). In this case, the KeyExtractor would be the Word.getValue method specified as a method reference (a lambda would also work).

But first, you should create a record or class to hold the word and value. That is so when you sort the records based on the value, the word and value will still be together as a unit.

record Word(String getWord, int getValue) {
     @Override
     public String toString() {
         return "%s -> %d".formatted(getWord, getValue);
     }
}

Here I have created a list of Word records. The values are arbitrary for demonstration.

List<Word> list = new ArrayList<>(
       List.of(new Word("the", 30), new Word("Hello", 20), new Word("GoodBye", 2)));

list.sort(Comparator.comparing(Word::getValue));
System.out.println(list);

prints

[GoodBye -> 2, Hello -> 20, the -> 30]

You can also sort in reverse order.

list.sort(Comparator.comparing(Word::getValue).reversed());
System.out.println(list);

prints

[GoodBye -> 2, Hello -> 20, the -> 30]

I would also suggest using a Map to hold the letters and their point value.

Map<String, Integer> letterMap = Map.of("A",1,"B",1,"C",1, ....);

Then you can access them like this.

int point = letterMap.get("A");
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
WJS
  • 36,363
  • 4
  • 24
  • 39
0

So you have a score calculating method:

int score(String word) {
    ...
}

And a list of words:

Path path = Paths.get("C:/words.txt");
List<String> words = Files.readAllLines(path, Charset.defaultCharset());

words.sort(Comparator.comparingInt(this::score)
                     .thenComparing(Function.identity()));

This will first sort on the score, and then alphabetically for the same score.

The comparing/comparingInt/ComparingDouble/... functions indicate the function that will give one comparison key (from the word). For the comparison of the words them selve Function.identity() will use the word itself.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138