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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:26+00:00 2026-05-23T15:39:26+00:00

Why does this code public class SponsoredComparator implements Comparator<SRE> { public boolean equals(SRE arg0,

  • 0

Why does this code

public class SponsoredComparator implements Comparator<SRE> {

    public boolean equals(SRE arg0, SRE arg1){
        return arg0.getSponsored()==arg1.getSponsored();
    }

    public int compare(SRE object1, SRE object2) {
        Log.d("SponsoredComparator","object1.getName() == "+ object1.getName());
        Log.d("SponsoredComparator","object1.getSponsored() == "+ object1.getSponsored());
        Log.d("SponsoredComparator","object2.getName() == "+ object2.getName());
        Log.d("SponsoredComparator","object2.getSponsored() == "+ object2.getSponsored());
        Log.d("SponsoredComparator","compare return == "+ (object1.getSponsored() && object2.getSponsored() ? 0 : object1.getSponsored() ? -1 : 1));
        return object1.getSponsored() && object2.getSponsored() ? 0 : object1.getSponsored() ? -1 : 1;
    }
}

throw this exception: ERROR/AndroidRuntime(244): java.lang.IllegalArgumentException: Comparison method violates its general contract!
ERROR/AndroidRuntime(4446): at java.util.TimSort.mergeLo(TimSort.java:743)

The method sre.getSponsored() returns a boolean.

Thanks.

  • 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-23T15:39:26+00:00Added an answer on May 23, 2026 at 3:39 pm

    I suspect the problem occurs when neither value is sponsored. That will return 1 whichever way you call it, i.e.

    x1.compare(x2) == 1
    
    x2.compare(x1) == 1
    

    That’s invalid.

    I suggest you change this:

    object1.getSponsored() && object2.getSponsored()
    

    to

    object1.getSponsored() == object2.getSponsored()
    

    in both places. I would probably actually extract this out a method with this signature somewhere:

    public static int compare(boolean x, boolean y)
    

    and then call it like this:

    public int compare(SRE object1, SRE object2) {
        return BooleanHelper.compare(object1.getSponsored(), object2.getSponsored());
    }
    

    That will make the code clearer, IMO.

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

Sidebar

Related Questions

Why does this code print 0? @Table(name = source) public class SourceDetails implements DatabaseEntity{
Why does this code not print an exception stack trace? public class Playground {
Why does this code: class A { public: explicit A(int x) {} }; class
Can you please tell me how does this java code work? : public class
Does this code solve the double checked locking issue in Java? public class DBAccessService()
This code: public class MyActivity extends Activity { private final boolean logging = getResources().getBoolean(R.bool.logging);
I see a lot of example code for C# classes that does this: public
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
So, why does this code: package org.popoffka.apicross; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public

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.