似乎无法按日期对对象数组进行排序

时间:2018-03-06 09:33:02

标签: java arrays object methods

class newPlanner{

    private  newAppointment[] Appoint = new newAppointment[20];

    newPlanner(){
        newAppointment obj1 = new newAppointment("Mar",4,17,30,"Quiz 1");

        newAppointment obj2 = new newAppointment("Apr",1,17,30,"Midterm");

        newAppointment obj3 = new newAppointment("May",6,17,30,"Quiz 2");

        newAppointment obj4 = new newAppointment("Jun",3,17,30,"Final");

        Appoint [0] = obj4;
        Appoint [1] = obj3;
        Appoint [2] = obj2;
        Appoint [3] = obj1;

    }

    public void runMethod(){
        boolean answer = true;

        while (answer) {
            System.out.println("Select an Option. ");
            System.out.println(" 1. Add Appointment ");  
            System.out.println(" 2. List Appointment ");
            System.out.println(" 3. Delete Appointment ");
            System.out.println(" 4. Exit ");

这是我的代码,我需要创建一个方法来移动数组元素以插入新约会,并删除约会。数组需要按日期排序,我似乎无法得到它请帮助

3 个答案:

答案 0 :(得分:0)

AppPoint使用ArrayList。编写自己的自定义比较器。使用Collections.sort(appPoint, new <CustomComparator>())

答案 1 :(得分:0)

您可以使用Arrays.sort(T[] a,Comparator<? super T> c)就地对数组进行排序,其中T是您的Appointment类。

您需要为Comparator实施Appointment界面。如果你的Appointment类在构造函数中使用了Date对象,这将会容易得多;然后你的比较器就是:

Arrays.sort(appointments, new Comparator<Appointment>{

    @Override
    public int compare(Appointment appointment1, Appointment appointment2){
        return appointment1.getDate().compareTo(appointment2.getDate());
    }
});

您可以按如下方式确定Appointment课程的日期:

public class Appointment{
    /**
    * three letter month
    */
    private final String month;

    /**
    * one to two digit day e.g. 1 or 12,
    */
    private final int day;

    /**
    * two digit military time hour e.g. 01, 11, 17
    */
    private final int hour;

    /**
    * lazily instantiated appointment Date
    */
    private Date date;

    //..

    public class Appointment(String month, int day, int hour /*other parameters*/){
        this.month = month;
        this.day = day;
        this.hour = hour;

        calculateDate();
    }

    private void caculateDate(){
        String dateString = String.format("%d %s 2018 %d",day,month,hour);
        SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy HH");

        try{
            this.date = dateFormat.parse(dateString);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public Date getDate(){
        return date;
    }
}

答案 2 :(得分:0)

public boolean compareAppointment (newAppointment A1, newAppointment A2) {

    String [] validMonth = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL",
                            "AUG", "SEP", "OCT","NOV","DEC"};

    int a=0;
    int b=0;

    for ( int i = 0; i< validMonth.length; i++) {
      if (A1.getMonth() == null ? validMonth[i] == null : A1.getMonth().equals(validMonth[i])) {
        a=i;
      }

      if (A2.getMonth() == null ? validMonth[i] == null : A2.getMonth().equals(validMonth[i])) {
        b = i;
      }
    }

    if (a!=b) {
      return a < b;
    }

    if (A1.getDay() != A2.getDay()) {
      return A1.getDay() < A2.getDay();
    }

    if (A1.getHourr()!= A2.getHourr()) {
      return A1.getHourr() < A2.getHourr();
    }

    if (A1.getMinn() != A2.getMinn()) {
      return A1.getMinn() < A2.getMinn();
    }

    return false;
}