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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:34:31+00:00 2026-05-19T15:34:31+00:00

I have an array of a custom type that I want to sort by

  • 0

I have an array of a custom type that I want to sort by one of its String attributes. For some reason, the following code is producing wrong results. Could you point out where I might have made a mistake?

class PatientLNComparator implements Comparator<Patient>{
        @Override
        public int compare(Patient p1, Patient p2) {
            String p1_LN = (p1 == null) ? null : p1.last_name;
            String p2_LN = (p2 == null) ? null : p2.last_name;

            if(p2_LN == null)
                    return -1;
            else if(p1_LN == null)
                    return +1;
            else if(p1_LN.equals(p2_LN))
                    return 0;
            else if(p1_LN.compareTo(p2_LN) > 0)
                    return -1;
            else
                    return +1;
        }
}
  • 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-19T15:34:32+00:00Added an answer on May 19, 2026 at 3:34 pm

    One problem to start with – your comparator is inconsistent if you give it two patients with null names, or two null patient references. In particular:

    Patient p1 = null;
    Patient p2 = null;
    
    int x = comparator.compare(p1, p2);
    int y = comparator.compare(p2, p1);
    

    The signs of x and y ought to be different – but they’ll both be -1.

    After that, it depends on how you want to compare the names. I would usually use

    return p1_LN.compareTo(p2_LN);
    

    if you want to sort in ascending order. Note that to sort in descending order you shouldn’t just return -p1_LN.compareTo(p2_LN), as if the comparison returns the Integer.MIN_VALUE, the negation won’t work. Instead you’d want to return p2_LN.compareTo(p1_LN);.

    Note that if you’re using this scheme, you don’t need to call p1_LN.equals(p2_LN) either – that will be handled by the compareTo call.

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

Sidebar

Related Questions

I have an array of custom objects. MyCustomArr[]. I want to convert this to
I have a custom object that I was adding to an array via a
I have a method that is adding custom objects to an array in a
i have a cck custom type that is created by people and the fields
I have the following code to build a custom taxonomy for my portfolio: add_action(
I have created a custom post type called banners and want to enable the
I'm currently studying WordPress and want to create a custom Post Type and have
So in my postgres DB I have the following custom type: create type my_pg_type
I have an array myArr which contains objects of custom class..e.g. objs of type
I have an array of an object type that has different text fields to

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.