5

I have to order Appointments by date and time. I have an ArrayList of Appointments and have tried to create a comparator to compare their dates and times. I am trying to use the Collections.sort method, passing it the ArrayList of Appointments and the AppointmentComparator I have created. When compiling I get a "No suitable method for sort." Here's a link to the full error message generated by the compiler : http://prntscr.com/7y4qb

Comparator:

public class AppointmentComparator implements Comparator<Appointment>
{
public int compare(Appointment a, Appointment b)
{
    if (a.getDay() < b.getDay())
        return -1;

    if (a.getDay() == b.getDay())
    {
        if (a.getStart() < b.getStart())
            return -1;
        if (a.getStart() > b.getStart())
            return 1;
        return 0;
    }

    return 1;
}

Line with syntax error:

Collections.sort(book, new AppointmentComparator());

variable book is an ArrayList of Appointments. ArrayList<Appointment>

AppointmentBook class:

import java.util.ArrayList;
import java.util.Collections;

public class AppointmentBook
{
private ArrayList<Appointment> book;

public AppointmentBook()
{
    book = new ArrayList<Appointment>();
}

public void addAppointment(Appointment appt)
{
    book.add(appt);
    Collections.sort(book, new AppointmentComparator());
}

public String printAppointments(int day)
{
    String list = "";

    for (int i = 0; i < book.size(); i++)
    {
        if (book.get(i).getDay() == day)
        {
            list = list + "Appointment description: " + book.get(i).getDescription() + "\n" + "Date of Appointment: " +
            book.get(i).getDay() + "\n" + "Time: " + book.get(i).getStart() + " - " + book.get(i).getEnd() + "\n" + "\n";
        }
    }

    return list;
}

Appointment class:

public class Appointment
{
private String desc;
private int day; //in format mmddyyyy
private int start; //in format hhmm
private int end; //in format hhmm

public Appointment(String description, int aptDay, int startTime, int endTime)
{
    desc = description;
    day = aptDay;
    start = startTime;
    end = endTime;
}

public String getDescription()
{
    return desc;
}

public int getDay()
{
    return day;
}

public int getStart()
{
    return start;
}

public int getEnd()
{
    return end;
}

}

22
  • 1
    Can you post the entire AppointmentComparator class for us? There may be a problem in the class definition itself, and we can only speculate without a compiler error/the class definition itself. Commented Apr 10, 2012 at 1:51
  • 1
    Can you also post how you defined the book variable before calling Collections.sort()? Commented Apr 10, 2012 at 1:52
  • Ok updated to include the entire class Commented Apr 10, 2012 at 1:55
  • We still need to see the declaration of book. Commented Apr 10, 2012 at 1:57
  • Well, book is an instance variable created within a class. It will be filled with Appointment objects specified by the user. Commented Apr 10, 2012 at 1:59

4 Answers 4

3

From the error message it looks like you forgot to declare your comparator as implementing the interface:

public class AppointmentComparator implements Comparator<Appointment> {}

It needs to have the implements part, not just contain the method.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry I didn't initially add that, but yes it was there and I still get the error
@user1323008 - recompile everything ? if not, post the full code
Ill post the full code to the AppointmentBook class, which is where I am having the error. I know for sure the Comparator is done correctly.
Ok original question is edited with the class that is giving the error message. Ignore the comments
Hmm, my next step would be to go through and make sure all existing .class files from old output are deleted before recompiling. (A "clean and build" usually, don't know how to do this in blueJ.) You could also try the explicit invocation of the method as Collections.<Appointment>sort(book, comparator); What compiler are you using?
1

You need to cast your new AppointmentComparator

Collections.sort(book, new (Comparator)AppointmentComparator());

Comments

1

Also we can use inner class for some cases:

public int indexOfLargest(ArrayList<QuakeEntry> Data) {
    Comparator<QuakeEntry> cmtr = new Comparator<QuakeEntry>() {
        @Override
        public int compare(QuakeEntry t, QuakeEntry t1) {
            if (t.getMagnitude() < t1.getMagnitude())
                return -1;
            if (t.getMagnitude() == t1.getMagnitude()) 
                return 1;
            if (t1.getMagnitude() > t1.getMagnitude())
                return 0;
        return 1;
        }
    };

    QuakeEntry max = Collections.max(Data, cmtr);
    int maxIndex = Data.indexOf(max);
    //----------------------------------------------------------------------
    System.out.println("//---------------------------------------------------");
    System.out.println("ArrayList sorted by Magnitude using inner class with Comparator");
    System.out.println("//---------------------------------------------------");
    Collections.sort(Data, cmtr);
    for (QuakeEntry qe : Data) {
        System.out.println(qe);
    }


    return maxIndex;
}

code for all classes: https://github.com/Evegen55/Java_DukeEdu_Coursera_2/blob/master/Earthquakes_Programming%20and%20Interfaces/src/earthquakes_programming_and_interfaces/QuakeEntry.java

Comments

0

Seems you have not implemented The comparator interface for your AppointmentComparator

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.