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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:56:56+00:00 2026-05-24T07:56:56+00:00

I have created a custom Comparator to sort an ArrayList of Strings. I have

  • 0

I have created a custom Comparator to sort an ArrayList of Strings. I have run it through the debugger and watched it comparing and returning values correctly. However, my array is not sorted. Since I am new to Java & Android, there might be something else going on.

After looking at it for a few hours, I can’t figure out what .. and since I have been using this site to answer so many other questions, I knew where to come to !

    Collections.sort(allWords, new Comparator<String>(){
        public int compare(String o1, String o2) {
            scoreWord sc1 = new scoreWord((String)o1);
            scoreWord sc2 = new scoreWord((String)o2);
            int i1 = sc1.getScore();
            int i2 = sc2.getScore(); 
            if ( i1 > i2 )
                return 1;
            return 0;
        }

        public boolean equals(String o1, String o2) {
            scoreWord sc1 = new scoreWord((String)o1);
            scoreWord sc2 = new scoreWord((String)o2);
            int i1 = sc1.getScore();
            int i2 = sc2.getScore(); 
            if ( i1 == i2 )
                return true;
            return false;
        }
     });
  • 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-24T07:56:57+00:00Added an answer on May 24, 2026 at 7:56 am

    Your compare method isn’t symmetric – it always either returns 1 or 0.

    Instead, just delegate to Integer.compare (if it’s available in the version of Java you’re using), passing in the scores:

    public int compare(String o1, String o2) {
        scoreWord sc1 = new scoreWord((String)o1);
        scoreWord sc2 = new scoreWord((String)o2);
        return Integer.compare(i1, i2);
    }
    

    Otherwise do it by hand, which is frankly a pain – if you need this in more than one place, I suggest you write your own implementation of Integer.compare to avoid the repetition:

    public int compare(String o1, String o2) {
        scoreWord sc1 = new scoreWord((String)o1);
        scoreWord sc2 = new scoreWord((String)o2);
        return i1 > i2 ? 1
             : i1 < i2 ? -1
             : 0;
    }
    

    This way you’ll have appropriate symmetry:

    • a.compareTo(b) < 0 implies b.compareTo(a) > 0
    • a.compareTo(b) > 0 implies b.compareTo(a) < 0
    • a.compareTo(b) == 0 implies b.compareTo(a) == 0
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom build tasks in my project file to run my nUnit
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have created a custom dialog for Visual Studio Setup Project using the steps
If have created a custom role within SqlServer which I added to the db__denydatareader
I have created a custom TitleWindow whcih i use as a popup. The contents
I have created a custom list as a feature in sharepoint. i need to
I have created a custom ExpandableListAdapter and everything works properly. What I'd like to
I have created a custom workflow activity that copies attachments from a case to
I have created a custom tag that looks like this: def textField = {

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.