我正在使用Composite设计模式进行文本存储。 例如:有一个文本,它有几个段落,每个段落包含几个句子,每个句子由一定数量的单词组成,而单词则由“叶”-字符组成。 我正在尝试创建一种方法,该方法将按'char'对句子中的单词进行排序。
public TextComponent sortBySymbol(TextComponent textComponent, char symbol) {
Comparator<TextComponent> comparator =
Comparator.comparingLong(o -> {
char[] word = textComponent.toString().toCharArray();
int counter = 0;
for (char ch : word) {
if (ch == symbol) counter++;
}
return counter;
});
comparator=comparator.reversed();
comparator= comparator.thenComparing(TextComponent::toString);
textComponent.sort(comparator, SENTENCE);
return textComponent;
}
@Override
public void sort(Comparator<TextComponent> comparator, TextType type) {
if (textType == type) {
textComponents.sort(comparator);
} else {
textComponents.forEach(o -> {
if (o.getTextType() != TextType.CHAR) {
o.sort(comparator, type);
}
});
}
}