Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:18:09+00:00 2026-06-06T12:18:09+00:00

I use Guava’s Ordering class to perfom sorting to pick the ‘best’ from a

  • 0

I use Guava’s Ordering class to perfom sorting to pick the ‘best’ from a given 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, the String “AEro” is the best because it’s the longest, joint-best with “fooz”, but tiebreaks with a higher percentage of uppercase characters.

I am looking for a way to tell which Comparator ‘broke the tie’, which in this silly contrived example would be the comparator PERCENTAGE_UPPERCASE.

I have a workable solution, but it’s not particularly elegant, and means duplicating the list of Comparators. It is to use the Ordering to provide a sorted list (Ordering.sortedCopy), pulling the first two elements (range checking of course), iterating through a List of the same Comparators, comparing those two elements, breaking when the compareTo method returns non-zero result.

Is there a neater way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:18:11+00:00Added an answer on June 6, 2026 at 12:18 pm

    Guava contributor here.

    Your solution seems like it’s nearly as good as you’re going to get, but instead of doing a sorted copy and pulling the first two elements, you should do the more efficient

    List<E> best2 = ranker.greatestOf(list, 2);
    

    and then, indeed, iterate through the comparators, though you can probably refactor so you’re reusing the list of comparators from Ordering.compound, not recreating it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use Google's Guava ImmutableSet class to create a set of immutable
The Guava developers chose to use annotations: class EventBusChangeRecorder { @Subscribe void recordCustomerChange(ChangeEvent e)
I'm trying to use AbstractScheduledService from google guava lib. In the docs for this
I have a java code where I want to use Guava libraries CharMatcher function.
I'm refactoring some code to use guava Cache . Initial code: public Post getPost(Integer
I use the following guava cache to store messages for a specific time waiting
I love Google Guava and use it a lot, but there is one method
I am just starting to us Guava in place of Google-Collections. The Splitter class
I use guava's EventBus, unfortunately it catches and logs the InvocationTargetException that occurs when
I'm starting to use the Guava classes and stumbling about the missing constructors. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.