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 6929511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:19:38+00:00 2026-05-27T11:19:38+00:00

I have class implementing Comparator<String> which should sort strings in such manner that strings

  • 0

I have class implementing Comparator<String> which should sort strings in such manner that strings with first word being roman number should be before string that don’t begin with roman number, and if both strings begin with roman literal, there should be two cases:

  1. sort by roman number and then if equal by rest of string
  2. sort by rest of string and if equal than roman number

I wanted to use Guava’s ComparisonChain but encountered two problems:

  1. During debug in Eclipse I can’t see what’s inside ComparisonChain object (even while being in ComparisonChain class).
  2. comparisonChain.result() in second example above always returns 0 (I can’t inspect flow because of problem #1) for values like “XI Something”, “III Something” etc. (conversion to long is OK – I checked values).

What is the problem here? I can use comparisonChain object like above (by making variable and adding .compares in some ifs like with builders), can’t I?

This code:

  if (romanComparisionFirst) {
    return ComparisonChain.start()
        .compare(Roman.toLong(leftFirstWord), Roman.toLong(rightFirstWord))
        .compare(WORDS_JOINER.join(leftWordsTail), WORDS_JOINER.join(rightWordsTail), collator)
        .result();
  } else {
    return ComparisonChain.start()
        .compare(WORDS_JOINER.join(leftWordsTail), WORDS_JOINER.join(rightWordsTail), collator)
        .compare(Roman.toLong(leftFirstWord), Roman.toLong(rightFirstWord))
        .result();
  }

and this:

  final ComparisonChain comparisionChain = ComparisonChain.start();
  if (romanComparisionFirst) {
    comparisionChain
        .compare(Roman.toLong(leftFirstWord), Roman.toLong(rightFirstWord))
        .compare(WORDS_JOINER.join(leftWordsTail), WORDS_JOINER.join(rightWordsTail), collator);
  } else {
    comparisionChain
        .compare(WORDS_JOINER.join(leftWordsTail), WORDS_JOINER.join(rightWordsTail), collator)
        .compare(Roman.toLong(leftFirstWord), Roman.toLong(rightFirstWord));
  }
  return comparisionChain.result();

give different results – first case is OK, second always 0. So question is: Can I use ComparisonChain like shown if second case or is this behavior a bug?

  • 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-05-27T11:19:38+00:00Added an answer on May 27, 2026 at 11:19 am

    Each compare method of the ComparisonChain returns a ComparisonChain, which is not necessarily the chain on which compare is invoked. The first snippet doesn’t ignore the result (and is thus correct), but the second one ignores the result, and is thus incorrect.

    It would be correct if it did:

    comparisionChain = comparisionChain.compare(...).compare(...);
    

    See http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/src-html/com/google/common/collect/ComparisonChain.html#line.89 for the source code.

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

Sidebar

Related Questions

I have a factory class that populates objects with data. I want to implementing
I have this interface and a class implementing it: interface Scraper { string DateToUrl(DateTime
I have one main interface and an abstract class implementing all derivable methods (that
I have some interface and class implementing that interface: public interface IWhatever { bool
I have an interface with several events I have base class implementing the interface
I have a class Image implementing ISerializable : [Serializable] [XmlRoot(ElementName = IMAGE)] [TypeConverter(typeof(ImageTypeConverter))] public
I have a third party assembly with a public abstract class implementing a certain
I have this question: Does implementing a custom MembershipProvider class needs you to implement
For my compsci class, I am implementing a Stack template class, but have run
I have class method that returns a list of employees that I can iterate

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.