Sorting a queue object by date and time (dd-MM-yyyy hh:mm:ss) with multiple instance variables #java?

I have a project due in 4 days, and I could not find a way to sort the queue object by its date. I have tried using collections.sort, but that only accepts variables, not objects. I have tried taking out the date from the queue object, but sorting the date with no ID (the instance variable) would be pointless. (Example : Sorting appointment date with no patient ID or doctor ID)

2 Answers

Relevance
  • John
    Lv 7
    6 months ago

    Collections.sort is correct and you need to implement a comparator,

    import java.util.*;

    import java.lang.*;

    import java.text.*;

    import java.util.stream.*;

    class Program

    {

    public static void main(String args[])

    {

    List<Record> records = build();

    List<Record> filtered =

    records.stream()

    .filter(i -> i.patientId.isEmpty() || i.doctorId.isEmpty())

    .collect(Collectors.toList());

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

    filtered.forEach( System.out::println);

    }

    private static List<Record> build() {

    List<Record> records = new ArrayList<Record>();

    records.add(new Record("", "", "04/01/2019 11:11:11"));

    records.add(new Record("", "", "03/01/2019 13:13:12"));

    records.add(new Record("1", "11", "01/01/2019 00:00:00"));

    records.add(new Record("", "11", "01/01/2019 00:00:02"));

    records.add(new Record("1", "", "01/01/2019 00:00:01"));

    return records;

    }

    }

    class Record {

    public String patientId;

    public String doctorId;

    public String appointmentDate;

    public Record(String patientId, String doctorId, String appointmentDate) {

    this.patientId = patientId;

    this.doctorId = doctorId;

    this.appointmentDate = appointmentDate;

    }

    @Override public String toString() {

    StringBuilder sb = new StringBuilder();

    sb.append("PatientId:").append( this.patientId);

    sb.append(" DoctorId:").append( this.doctorId);

    sb.append(" AppointmentDate:" ).append(this.appointmentDate);

    return sb.toString();

    }

    }

    class AppointmentComparator implements Comparator<Record> {

    @Override public int compare(Record a, Record b) {

    Calendar calA = this.getAsCalendar(a.appointmentDate);

    Calendar calB = this.getAsCalendar(b.appointmentDate);

    return (calA.after(calB) ? 1 : -1);

    }

    private Calendar getAsCalendar(String dateString) {

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

    Calendar cal = Calendar.getInstance();

    try {

    cal.setTime(sdf.parse(dateString));

    } catch (ParseException ignore) {}

    return cal;

    }

    }

    • Login to reply the answers
  • 6 months ago

    The appointment class has these variable

    (String) Patient ID

    (String) Patient Name

    (String) Doctor ID

    (String) Doctor Name

    (String) Appointment Date //(dd-MM-yyyy hh:mm:ss)

    (String) Patient Symptom

    #Queue can be changed to LinkedList

    • Login to reply the answers
Still have questions? Get your answers by asking now.