简体   繁体   中英

Sorting arraylist of string arrays Java

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. Eg 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.

First, there is no need to convert the array to List in order to sort.

Second, you should use thenComparing(Comparator.naturalOrder()) instead of thenComparing(String::compareTo) , as that will use a singleton Comparator , rather than creating a new Comparator delegating to the method reference.

As for your question, I don't think there exists a Comparator for that, so just create your own. The building of a compound Comparator using thenComparing() is nice, but not always the right way to go.

So, your code could be (assuming I got your sort criteria right) :

List<String[]> solutions = /* code here */;

// First, sort each individual solution (array)
for (String[] solution : solutions) {
    Arrays.sort(solution, Comparator.comparing(String::length)
                                    .reversed()
                                    .thenComparing(Comparator.naturalOrder()));
}

// Second, sort the solutions (list)
solutions.sort((solution1, solution2) -> {
        // 1) By number of words (ascending)
        int cmp = Integer.compare(solution1.length, solution2.length);
        // 2) By length of word (descending)
        for (int i = 0; cmp == 0 && i < solution1.length; i++)
            cmp = Integer.compare(solution2[i].length(), solution1[i].length());
        // 3) Alphabetically (ascending)
        for (int i = 0; cmp == 0 && i < solution1.length; i++)
            cmp = solution1[i].compareTo(solution2[i]);
        return cmp;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM