Использование компаратора для упорядочения ArrayList Java

Мне нужно упорядочить встречи по дате и времени. У меня есть список назначений ArrayList, и я попытался создать компаратор для сравнения их дат и времени. Я пытаюсь использовать метод Collections.sort, передавая ему ArrayList of Appointments и созданный мной AppointmentComparator. При компиляции получаю "Нет подходящего метода для сортировки". Вот ссылка на полное сообщение об ошибке, сгенерированное компилятором.:http://prntscr.com/7y4qb

Компаратор:

public class AppointmentComparator implements Comparator
{
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;
}

Строка с синтаксической ошибкой:

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

Книга переменных представляет собой список ArrayList назначений.ArrayList

Класс AppointmentBook:

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

public class AppointmentBook
{
private ArrayList book;

public AppointmentBook()
{
    book = new ArrayList();
}

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:

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;
}

}

5
задан shaanIQ 10 April 2012 в 02:19
поделиться