I have a program that takes a word and a text file dictionary, and searches the dictionary for combinations of words that equal (are anagrams of) the given word.
I end up with an Arraylist of String arrays, each array is a solution containing the words it used, and the Arraylist is all solutions.
I then iterate over the arraylist and sort the arrays as:
List<String> list = Arrays.asList(array);
list.sort(Comparator.comparing(String::length).reversed().thenComparing(String::compareTo));
which sorts first by word length (descending) and then uses alphabetical as a tie breaker for equal length words.
I now have the individual arrays sorted but i'm trying to sort them within the arraylist by certain rules:
- by ascending number of words
- for arrays that contain equal number of words, and all words are of same length, the arrays are alphabetically sorted.
- equal number of words, but differing lengths: longest non-equal length first. E.g if a[0] length == b[0] length but b[1] length>a[1] length, b comes first.
They are already stored by ascending number of words as single word solutions are found first, then 2 word and so on, being appended to the arraylist.
Now as the arrays are in descending word length order too after sorting, i'm thinking there must be a straightforward Comparator to achieve the above but i'm struggling to do it.