通过辅音Java比较和排序Word实体(以元音符号开头)

时间:2017-12-23 13:22:02

标签: java sorting comparator

我有一个类符号,单词,句子,文本的层次结构。每个类只包含一个带有setter,getter,重写的equals和toString的字段。这些类是通过列表彼此嵌套的。它看起来像这样:

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

我需要在文本中找到从元音字母开始的单词,并按照元音后面的辅音字母对它们进行排序。我一直试图从文本中获取句子和单词,然后定义我需要排序的单词。但是,我不知道如何在我的辅助方法排序中更改比较,因此它实际上可以比较辅音字母。

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!
        });
    }

输入: 让我们假设这里有某种文字。虽然我不确定你会在这些词中找到一些意义,但有文字。它应该是关于编程但我没有弄清楚要写什么。

预期输出(从元音开始的单词按辅音的第一次出现排序): 我,关于,虽然,想象,我,在,是,是,它,确实

我的输出还没有输出,因为我还没有完成方法

1 个答案:

答案 0 :(得分:0)

我不知道您的Symbol类的详细信息,因此我留下了部分代码供您填写。如果比较结果为0,您可能还想对单词进行排序(Comparator<String>会有帮助)。

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)
        }
    });
}