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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:22:35+00:00 2026-05-20T09:22:35+00:00

I’m calling Arrays.sort(schedule, c); where c is an instance of a comparator like so:

  • 0

I’m calling Arrays.sort(schedule, c); where c is an instance of a comparator like so:

import java.util.Comparator;

public class FirstOccComparator implements Comparator<AbstractEvent> {

    public int compare(AbstractEvent event1, AbstractEvent event2) {        
          int result = 0;       
          if (event1 == null || event2 == null) {
              //System.out.println("null");
          }
          else if (event1.hasMoreOccurrences() && event2.hasMoreOccurrences()) {
            result = event1.nextOccurrence().compareTo(event2.nextOccurrence());
          }
          return result;
    }

}

The output I’m getting isn’t what it’s supposed to be. I’m wondering if someone can point me in the right direction here. This is the first sorting algorithm I’ve ever made and its using concepts that are still new to me (comparators and implementation), so sorry about the multiple questions regarding my code 🙂

EDIT
This is the difference between the outputs: http://pastebin.com/LWy1jqkt

There are two kinds of events, these are the hasMoreOccurrences() and nextOccurrence() methods:

DailyEvent

public boolean hasMoreOccurrences() {
        boolean result = false;
        Date check = nextOccurrence();
        timesCalled--;
        if (check instanceof Date && check != null) {
            result = true;
        }
        return result;
    }

public Date nextOccurrence() {
        if (timesCalled > recurrences) {
            return null;
        }
        else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(startTime);
            cal.add(Calendar.DATE, timesCalled);
            timesCalled++;
            return cal.getTime();
        }
    }

WeeklyEvent

public boolean hasMoreOccurrences() {
        Date tmp = nextOccurrence();
        timesCalled--;
        boolean result = false;
        if (tmp instanceof Date && tmp != null) {
            result = true;
        }

        return result;
    }

public Date nextOccurrence() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        cal.add(Calendar.DATE, timesCalled*7);

        if (cal.getTime().compareTo(this.endTime) > 0) {
            return null;
        }
        else {
            timesCalled++;
            return cal.getTime();
        }
    }
  • 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-20T09:22:36+00:00Added an answer on May 20, 2026 at 9:22 am

    There are a few things that seem incorrect with your comparator.

    For instance, what happens if only one of them is null? How do you want those to sort? Right now you are considering two events equal if one of them is null.

    Also, what happens if one event has more occurences while the other does not? Right now you only do comparisons on occurrences if both events have more occurrences. You need to handle the case where one has occurences while the other does not.

    Also, if the occurence is a custom class, you need to evaluate that comparator as well.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.