首先按照最近或即将到来的日期排序,然后根据今天的日期排序

时间:2017-10-18 15:52:22

标签: java android list sorting date

列表的订购方式应该是项目的日期应该接近今天的日期,如果它已经过了今天的日期并且距离最近那么它应该结束。 例如:

  

日期 - 2017年3月10日12/9/2017 20/10/2017 21/10/2017

假设这是从数据库中检索的日期列表,当添加到自定义适配器时,将以相同的顺序显示listview项。 但我希望他们被订购(考虑今天的日期为 - 2017年10月18日)

  

20/10/2017   21/10/2017   (因为即将到来的日期在前两个项目结束,现在通过了   日期列表)2017年3月10日12/9

以下是使用的类

AssignmentUpload.java

public AssignmentUpload(String fileName, String url, String year, String title,
                        String instructions, AttendanceDate attendanceDate, String subjectName, String uploadedBy) {
    this.fileName = fileName;
    this.url = url;
    this.year = year;
    this.title = title;
    this.instructions = instructions;
    this.attendanceDate = attendanceDate;
    this.subjectName = subjectName;
    this.uploadedBy = uploadedBy;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getInstructions() {
    return instructions;
}

public void setInstructions(String instructions) {
    this.instructions = instructions;
}

public AttendanceDate getAttendanceDate() {
    return attendanceDate;
}

public void setAttendanceDate(AttendanceDate attendanceDate) {
    this.attendanceDate = attendanceDate;
}

public String getSubjectName() {
    return subjectName;
}

public void setSubjectName(String subjectName) {
    this.subjectName = subjectName;
}

public String getUploadedBy() {
    return uploadedBy;
}

public void setUploadedBy(String uploadedBy) {
    this.uploadedBy = uploadedBy;
}}

AttendanceDate.java用于日期

public class AttendanceDate implements Serializable{

private int day, month, year;

public AttendanceDate() {
}

public AttendanceDate(int day, int month, int year) {
    this.day = day;
    this.month = month;
    this.year = year;
}

public int getDay() {
    return day;
}

public void setDay(int day) {
    this.day = day;
}

public int getMonth() {
    return month;
}

public void setMonth(int month) {
    this.month = month;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

@Override
public String toString() {
    return "AttendanceDate{" +
            "day=" + day +
            ", month=" + month +
            ", year=" + year +
            '}';
}}

FacultyViewAssignmentActivity.java有要编辑的loadAssignmentList()函数

private void loadAssignmentsList() {
    pDialog.setTitle("Searching...");
    pDialog.setMessage("Enjoy Assignments... :)");
    pDialog.show();
    reference.child("assignments")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    assignmentUploadList.clear();
                    for (DataSnapshot ps : dataSnapshot.getChildren()) {
                        for (DataSnapshot ds : ps.getChildren()) {
                            assignmentYear.setSubjectName(ds.getValue().toString());
                            for (DataSnapshot dss : ds.getChildren()) {
                                AssignmentUpload a = dss.getValue(AssignmentUpload.class);
                                assignmentUploadList.add(a);
                            }
                        }
                    }
                    assAdapter = new FacultyViewAssignmentAdapter(FacultyViewAssignmentActivity.this, assignmentUploadList);
                    recyclerView.setAdapter(assAdapter);
                    pDialog.dismiss();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    pDialog.dismiss();
                }
            });
}

长期以来一直坚持这一点,任何帮助都会受到赞赏...... 谢谢!

2 个答案:

答案 0 :(得分:0)

您需要根据需要对日期进行排序的比较器如下所示:

Comparator<LocalDate> c = (date1, date2) -> {
               boolean isPast1 = date1.isBefore(LocalDate.now());
               boolean isPast2 = date2.isBefore(LocalDate.now());

               if (isPast1 != isPast2)
                   return isPast1 ? 1 : -1;

               return isPast1 ? date2.compareTo(date1) : date1.compareTo(date2);
             });

它首先将日期分为现在之前和之后,如果它们在同一组中,它们会自然排序(反向)。

我在评论中描述了如何将其应用于您的上传内容:

uploads.sort(Comparator.comparing(upload -> LocalDate.of(upload.getAttendanceDate().getYear(), upload.getAttendanceDate().getMonth(), upload.getAttendanceDate().getDay()),
             (date1, date2) -> {
                 boolean isPast1 = date1.isBefore(LocalDate.now());
                 boolean isPast2 = date2.isBefore(LocalDate.now());

                 if (isPast1 != isPast2)
                     return isPast1 ? 1 : -1;

                 return isPast1 ? date2.compareTo(date1) : date1.compareTo(date2);
             }));

通过这个,您可以使用上面的比较器(即首先将过去和未来分开,然后对每个组进行排序),按照日期对上传列表进行排序。

答案 1 :(得分:-1)

如果您创建两个已排序的列表,一个包含即将到来的日期,另一个包含已经过的日期,该怎么办?然后首先填写第三个列表,包括即将到来的日期,然后是过去的日期。 : -

TestCode.java

package com.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class TestCode {

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date today = sdf.parse("18/10/2017");

        // assuming Dates - 3/10/2017 12/9/2017 20/10/2017 21/10/2017
        String dts = "3/10/2017,12/9/2017,20/10/2017,21/10/2017";

        List<AssignmentUpload> assignmentUploadList = new ArrayList<AssignmentUpload>();
        List<AssignmentUpload> futureAssg = new ArrayList<AssignmentUpload>();
        List<AssignmentUpload> pastAssg = new ArrayList<AssignmentUpload>();
        List<AssignmentUpload> finalAssg = new ArrayList<AssignmentUpload>();

        for (String dt : dts.split(",")) {

            String[] nos = dt.split("/");
            int day = Integer.parseInt(nos[0]);
            int mon = Integer.parseInt(nos[1]);
            int year = Integer.parseInt(nos[2]);

            AttendanceDate ad = new AttendanceDate(day,mon,year);
            AssignmentUpload au = new AssignmentUpload();
            au.setAttendanceDate(ad);
            au.setSubjectName("assg - " +dt);

            assignmentUploadList.add(au);

        }

        // Now we will start with assignmentUploadList

        for (AssignmentUpload au : assignmentUploadList) {

            int day = au.getAttendanceDate().getDay();
            int mon = au.getAttendanceDate().getMonth();
            int year = au.getAttendanceDate().getYear();

            Date date = sdf.parse(day+"/"+mon +"/"+year); 

            if (date.before(today)) {
                pastAssg.add(au);
            } else {
                futureAssg.add(au);
            }

        }

        // Sort pastand future dates individually
        Collections.sort(futureAssg);
        Collections.sort(pastAssg,Collections.reverseOrder());

        // Add future Dates to final Dates first
        finalAssg.addAll(futureAssg);


        // Then Add past Dates to final Dates
        finalAssg.addAll(pastAssg);

        assignmentUploadList = finalAssg;

        for (AssignmentUpload au : assignmentUploadList) {
            System.out.println(au.getSubjectName());
        }

    }

}

更新了AssignmentUpload.java(使其具有可比性)

package com.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.test.AttendanceDate;

public class AssignmentUpload implements Comparable{

    private String fileName;
    private String url;
    private String year;
    private String title;

    private String instructions;
    private AttendanceDate attendanceDate;
    private String subjectName;
    private String uploadedBy;
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getInstructions() {
        return instructions;
    }
    public void setInstructions(String instructions) {
        this.instructions = instructions;
    }
    public AttendanceDate getAttendanceDate() {
        return attendanceDate;
    }
    public void setAttendanceDate(AttendanceDate attendanceDate) {
        this.attendanceDate = attendanceDate;
    }
    public String getSubjectName() {
        return subjectName;
    }
    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }
    public String getUploadedBy() {
        return uploadedBy;
    }
    public void setUploadedBy(String uploadedBy) {
        this.uploadedBy = uploadedBy;
    }

    public int compareTo(Object o) {

        AssignmentUpload au = (AssignmentUpload)o;

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");



        int day = attendanceDate.getDay();
        int mon = attendanceDate.getMonth();
        int year = attendanceDate.getYear();

        int oday = au.getAttendanceDate().getDay();
        int omon = au.getAttendanceDate().getMonth();
        int oyear = au.getAttendanceDate().getYear();

        try {
            Date date = sdf.parse(day+"/"+mon +"/"+year);
            Date odate = sdf.parse(oday+"/"+omon +"/"+oyear);

            return date.compareTo(odate);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 0;
    }



}

输出: assg - 20/10/2017 assg - 21/10/2017 assg - 3/10/2017 assg - 12/9/2017