Using Comparator to order ArrayList Java

18,371

Solution 1

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.

Solution 2

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

Solution 3

You need to cast your new AppointmentComparator

Collections.sort(book, new (Comparator)AppointmentComparator());
Share:
18,371
shaanIQ
Author by

shaanIQ

Updated on June 04, 2022

Comments

  • shaanIQ
    shaanIQ about 2 years

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

    }