Cassie
Cassie

Reputation: 349

Compare and sort Word entities (starting with vowel symbols) by consonants Java

I have a hierarchy of classes Symbol, Word, Sentence, Text. Each class just contains a field with setters, getters, overridden equals and toString. These classes are kind of nested in each other by lists. It looks like this:

public class Word {
    private List<Symbol> symbols;
    public Word(List<Symbol> symbols) {
        this.symbols = symbols;
    }
    //getters, setters, toString
}

I need to find the words in the text which start from the vowel letter and sort them according to the consonant letter which goes after the vowel. I have been trying to get sentences and words from the text and then defined the words I need to sort. However, I do not know how to change compare in my helper method sort so it could actually compare consonant letters.

public void sortWords(Text text) {
        List<Sentence> sentences = text.getSentences();
        List<Word> words = new ArrayList<>();
        for (Sentence sentence : sentences) {
            words.addAll(sentence.getWords());
        }
        List<Symbol> symbols = new ArrayList<>();
        for (Word word : words) {
            symbols = word.getSymbols();
            Symbol first = symbols.get(0);
            if (first.isVowel()) {
                sort(words);
            }
        }
    }


    private void sort(List<Word> words) {
        Collections.sort(words, new Comparator<Word>() {

            @Override
            public int compare(Word word1, Word word2) {
                List<Symbol> symbols = word1.getSymbols();

                for (Symbol s : symbols) {
                    if (s.isConsonant()){
                        //smth should be here
                    }
                }
            }

I would be grateful for any advice!
        });
    }

Input: Let's imagine that there is some kind of text here. Although I am not sure that you will find some sense in these words but there is text. It should be about programming but I did not figure out what to write exactly.

Expected output (words starting from vowels are sorted by first occurrence of consonants): I, about, of, although, imagine, am, in, is, is, it, exactly

My output: there is not output yet because I have not finished the method

Upvotes: 0

Views: 623

Answers (1)

O.O.Balance
O.O.Balance

Reputation: 3040

I don't know the details of your Symbol class, so I have left part of the code for you to fill in. If the comparison results in 0, you may want to still sort the words (Comparator<String> will help).

public void sortWords(Text text) {
    List<Sentence> sentences = text.getSentences();
    List<Word> words = new ArrayList<>();
    for (Sentence sentence : sentences) {
        words.addAll(sentence.getWords());
    }
    List<Word> wordsStartingWithAVowel = new ArrayList<>();
    for (Word word : words) {
        if (word.getSymbols().get(0).isVowel()) {
            wordsStartingWithAVowel.add(word);
        }
    }
    sort(wordsStartingWithAVowel);
}


private void sort(List<Word> words) {
    Collections.sort(words, new Comparator<Word>() {
        @Override
        public int compare(Word word1, Word word2) {
            int result;
            Symbol firstConsonantWord1 = null; // get the first consonant of the first word
            List<Symbol> symbols = word1.getSymbols();
            for (Symbol s : symbols) {
                if (s.isConsonant()){
                    firstConsonantWord1 = s;
                    break;
                }
            }
            Symbol firstConsonantWord2 = null; // get the first consonant of the second word
            List<Symbol> symbols = word2.getSymbols();
            for (Symbol s : symbols) {
                if (s.isConsonant()){
                    firstConsonantWord2 = s;
                    break;
                }
            }
            if(firstConsonantWord1 == null && firstConsonantWord2 == null) // both words don’t contain any consonants
                result = 0;
            else if(firstConsonantWord1 == null)
                result = -1;
            else if(firstConsonantWord2 == null)
                result = 1;
            else { // both words contain at least one consonant
                result = new Comparator<Symbol>(){
                    @Override
                    public int compare(Symbol symbol1, Symbol symbol2) {
                        // insert comparison of symbols here, depends on your Symbol class
                    }
                }.compare(firstConsonantWord1, firstConsonantWord2);
            }
            // if result is 0 you may want to do further comparisons (like a standard String comparison)
        }
    });
}

Upvotes: 0

Related Questions