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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:02:14+00:00 2026-05-30T13:02:14+00:00

I am implementing my own Java class for sorting a List<T> in eclipse. I

  • 0

I am implementing my own Java class for sorting a List<T> in eclipse. I made break points in comparsion statements and it does not work as I unexpected!

Here is the code :

if(doy2 < don && doy1>don)
        {
            return 1;
        }
        else if (doy2 > don && doy1<don)
        {
            return -1;  
        }
        else
        {
            return 0;
        }

Even though the doy2 > don && doy1<don statement evaluates to true, and the code reaches return -1, but it also goes to else part and return 0. Why exactly?

Edited: The Complete code

public class DateCompartor implements Comparator<BirthdayContact> {

    @Override
    public int compare(BirthdayContact arg0, BirthdayContact arg1) {

    Date now=new Date();

        Date bd1=arg0.GetBirthDay();
        Date bd2=arg1.GetBirthDay();

            DateTime dt1=new DateTime(bd1);
        DateTime dtnow=new DateTime(now);
        DateTime dt2=new DateTime(bd2);

        int doy1=dt1.getDayOfYear();
        int doy2=dt2.getDayOfYear();
        int don=dtnow.getDayOfYear();

        if(doy2 < don && doy1>don)
        {
            return 1;
        }
        else if (doy2 > don && doy1<don)
        {
            return -1;  
        }
        else
        {
            return 0;
        }
    }
}

I am comparing two dates with current date, and which ever is closer to current date, should get upper in the list.

  • 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-30T13:02:15+00:00Added an answer on May 30, 2026 at 1:02 pm

    I’d rebuild your code and step through it again. The ‘weirdness’ that you note might be due to the source and bytecode being out of synch.

    I’d ask that you post the entire Comparable implementation. Comparator usually compares two objects, but your comparison statements appear to involve three. Perhaps you can clarify exactly what you’re doing.

    Be assured that your code is incorrect. You have a mental model of how this should work in mind, but your assumptions don’t match reality. Your job is to bring them back into harmony.

    UPDATE:

    Given the new information, I’d certainly not code it the way you did. I’d calculate the difference from each date to today’s time and compare those. I’m not interested enough to test it out myself. See if that works out better for you.

    public class DateCompartor implements Comparator<BirthdayContact> {
    
        @Override
        public int compare(BirthdayContact arg0, BirthdayContact arg1) {
    
            Date now=new Date();    
            // Read Sun's Java coding standards; these don't follow the standard.                         
            Date bd1=arg0.GetBirthDay();  
            Date bd2=arg1.GetBirthDay();
            long dist1 = Math.abs(bd1.getTime() - now.getTime());
            long dist2 = Math.abs(bd2.getTime() - now.getTime());
            return dist1.compareTo(dist2); // Might need wrapper Long here.
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing my own linked list in Java. The node class merely has
I'm currently playing with implementing various sorting algorithms in Java, mostly for fun, but
I am having some trouble with implementing my own observer in Java on the
I have written my own java.util.List implementation, and now i want to store it
Im implementing my own proto in Java, it will use a heartbeat over a
Microsoft like implementing their own versions of popular open-source frameworks and assemblies, for example:
I've been implementing my own version of a red-black tree, mostly basing my algorithms
I'm implementing my own NSBitmapImageRep (to draw PBM image files). To draw them, I'm
Where can I get info about implementing my own methods that have the ellipsis
As a learning excercise, I've just had an attempt at implementing my own 'merge

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.