java - Android - 如何按日期对 arrayList 进行排序?

标签 java android sorting arraylist

我正在尝试按日期对我的 arrayList 进行排序。每次收到通知并从那里检索时,我都会将日期存储在 Firebase 上。我正在使用 Collections.sort 但我不知道如何在我的代码中实现,因为我的日期格式以字符串格式的数字开头,例如“2017 年 7 月 12 日凌晨 12:00”。

我在 stackoverflow 上看过一些这样的例子,但我不知道如何让它在我的案例中起作用。我将在下面发布屏幕截图和我的代码。

日期未排序。

enter image description here

NotificationFragment.java

public class NotificationFragment extends Fragment {
        prepareNotification1();
        sortDate();
        return v;
    }

       private void prepareNotification1() {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            String userID = user.getUid();

  mRef.child("customers").child(userID).child("Notification").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Notification menu = dataSnapshot.getValue(Notification.class);
                    notificationList.add(menu);
                mAdapter.notifyDataSetChanged();
            }
 });
    }

    public void sortDate() {
        Collections.sort(notificationList, new Comparator<Notification>() {
            @Override
            public int compare(Notification lhs, Notification rhs) {
                return lhs.getDate().compareTo(rhs.getDate());
            }
        });
        mAdapter = new NotificationAdapter(getContext(), notificationList);
        mRecyclerView.setAdapter(mAdapter);
    }
}

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Calendar cal = Calendar.getInstance();

        String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

        SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
        String currentTime = df.format(cal.getTime());

        String notificationTime = currentDateTimeString + " at " + currentTime;

        Notification newNotification = new Notification(remoteMessage.getData().get("body"), notificationTime);
        mRef.child("customers").child(userID).child("Notification").push().setValue(newNotification);
}

Notification.java

public class Notification {
    private String message;
    private String date;


    public Notification(){
    }

    public Notification(String message, String date){
        this.message = message;
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

最佳答案

解析日期后进行排序

Collections.sort(notificationList, new Comparator<Notification>() {
            DateFormat f = new SimpleDateFormat("dd/MM/yyyy '@'hh:mm a");
            @Override
            public int compare(Notification lhs, Notification rhs) {
                try {
                    return f.parse(lhs.getDate()).compareTo(f.parse(rhs.getDate()));
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        })

如果您的日期是其他格式,请相应地编写DateFormat

关于java - Android - 如何按日期对 arrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45049402/

相关文章:

java - 如何只有一个驱动程序可以用作Webdriver或Appiumdriver

java - 在 Java 中从 MongoDB 读取 Integer 字段时出现长类型转换错误

android - 如何在android中隐藏一个Layout控件

android - Samsung Galaxy ICS 4.0.3 LPQ 固件发送两条短信的任何解决方案?

android - 为屏幕方向保存我的 ListView 的状态

java - 使用类型继承的数字组层次结构

java - 不同的手机/不同的图像质量

php - 在一个或很少的查询中更新多个 MySQL 行的显示顺序

c++ - 使用 C++ 中的可选输入文件命令行解析方法在大文件中查找 X 最大值

python - 按链接名称对链接列表进行排序