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

  • Home
  • SEARCH
  • 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 7278887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:56:13+00:00 2026-05-28T22:56:13+00:00

I am getting the Comparison method violates its general contract exception with this compareTo

  • 0

I am getting the Comparison method violates its general contract exception with this compareTo method, but I can’t track down what is exactly causing the issue. I am trying to sort files by their extension in a particular way. Mind you this doesn’t happen for all phones, just ones that I don’t have access to which makes it harder to test out.

public int compareTo(NzbFile another) 
{
    if (this.getFileName() != null && another.getFileName() != null)
    {
        if (this.getFileName().toLowerCase().endsWith(".nfo"))
            return -1000;
        else if (another.getFileName().toLowerCase().endsWith(".nfo"))
            return 1000;
        else if (this.getFileName().toLowerCase().endsWith(".sfv"))
            return -999;
        else if (another.getFileName().toLowerCase().endsWith(".sfv"))
            return 1001;
        else if (this.getFileName().toLowerCase().endsWith(".srr"))
            return -998;
        else if (another.getFileName().toLowerCase().endsWith(".srr"))
            return 1002;
        else if (this.getFileName().toLowerCase().endsWith(".nzb"))
            return -997;
        else if (another.getFileName().toLowerCase().endsWith(".nzb"))
            return 1003;
        else if (this.getFileName().toLowerCase().endsWith(".srt"))
            return -996;
        else if (another.getFileName().toLowerCase().endsWith(".srt"))
            return 1004;
        else
            return this.getFileName().compareTo(another.getFileName());
    }
    else if (this.getFileName() != null && another.getFileName() == null)
    {
        return -995;
    }
    else if (this.getFileName() == null && another.getFileName() != null)
    {
        return 1005;
    }
    else
    {
        return this.getSubject().compareTo(another.getSubject());
    }

}
  • 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-28T22:56:14+00:00Added an answer on May 28, 2026 at 10:56 pm

    If your file names are the same, and both e.g. end in .nfo, then this will return that they aren’t equal. I assume that they’re meant to be equal.

    I strongly suspect that there’s a Better Way to Do This. I’ll use Guava for my example, but it’s not strictly necessary.

    static final List<String> EXTENSIONS = ImmutableList.of("nfo", "sfv", "srr", "nzb", "srt");
    final Ordering<String> fileNameOrdering = new Ordering<String>() {
      public int compare(String a, String b) {
        String extA = Files.getFileExtension(a);
        String extB = Files.getFileExtension(b);
        int extAIndex = EXTENSIONS.indexOf(extA);
        int extBIndex = EXTENSIONS.indexOf(extB);
        if ((extAIndex >= 0) == (extBIndex >= 0)) { // if they are both known extensions or both unknown
          return extAIndex - extBIndex;
        } else if (extAIndex < 0) { // a is unknown, b is known
          return -1;
        } else if (extBIndex < 0) { // b is unknown, a is known
          return 1;
        }
        return a.compareTo(b);
      }
    }.nullsLast();
    return new Ordering<NzbFile>() {
      public int compare(NzbFile a, NzbFile b) {
        return ComparisonChain.start()
          .compare(a.getFileName(), b.getFileName(), fileNameOrdering)
          .compare(a.getSubject(), b.getSubject())
          .result();
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a Comparison Method violates its general contract after compiling some Java
Sorting five integers using bitwise or comparison operators can be achieved by first getting
I am getting this error Argument \\x{61} isn't numeric in numeric comparison (<=>) from
I've had no real luck getting a concise answer for this comparison by using
Getting two warning when i run this program and i can't figure out how
real newbie question but why doesn't this work? I am getting use of unassigned
I am getting this warning: Possible unintended reference comparison; to get a value comparison,
I am getting this warning: warning: comparison between pointer and integer when doing the
Getting started with NHibernate How can I generate identity fields in nHibernate using Hilo
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler

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.