I use the Guava Ordering class to sort the sort to select the "best" from this list. It looks something like this:
// Create the Ordering, with a list of Comparators Ordering<String> ranker = Ordering.compound(ImmutableList.of( STRING_LENGTH, PERCENTAGE_UPPERCASE, NUMBER_OF_VOWELS)); // Use the ordering to find the 'best' from a list of Strings String best = ranker.max(asList("foo", "fooz", "Bar", "AEro"));
With this Ordering string "AEro" is the best, because it is the longest, joint with "fooz", but tie-breaks with a higher percentage of characters in uppercase.
I’m looking for a way to find out which Comparator “broke a tie”, which in this silly far-fetched example would be the PERCENTAGE_UPPERCASE comparator.
I have a workable solution, but it is not particularly elegant and means duplication of the list of Comparator s. It should use Ordering to provide a sorted list ( Ordering.sortedCopy ), pulling the first two elements (range checking, of course), iterating through the List of the same Comparator s, comparing these two elements, breaking when the compareTo method returns a non-zero result.
Is there an easier way?
source share