无法在Java/android中的现有比较器中添加对日期进行排序的逻辑 [英] Unable to add a logic to sort with dates in the existing comparator in Java/android

查看:70
本文介绍了无法在Java/android中的现有比较器中添加对日期进行排序的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个条件,该条件也根据item.startReading的DATE(降序)进行排序. item.startReading值具有字符串格式2018-10-20T12:05:41的日期值.但我不知道如何 将此逻辑添加到当前代码中.在用户单击杂志或书籍项目之前,item.startReading的值是null. .当用户单击其中一个项目时,我希望最新的item.startReading像下面一样转到顶部. 抱歉,我的解释很抱歉,但是自从我开始学习java/android以来,我真的需要知道该怎么做.一些例子或技巧会很可爱. 我希望收到您的来信.

I want to add a condition that also sorts according to the DATE (desc order) of the item.startReading. item.startReading value has the date value in the string format 2018-10-20T12:05:41. But I have no clue how to add this logic to the current code. Before the user clicks the magazine or the book item, the value of item.startReading is null. . When the user clicks the either one of the items, I want the newest item.startReading to go to the top like the following. Sorry for my explanation, but I really need to know how to do since I started learning java/android. Some examples or tips would be lovely. I would love to hear from you.

  1. 杂志(2018-10-21T13:06:41)最新的日期项转到顶部
  2. 杂志(2018-10-20T12:05:41)
  3. 书籍
  4. 书籍

目前,我有以下比较器代码,其排序如下.

Currently, I have the following comparator code that does the sorting like below.

(在使用比较器之前)

  1. 杂志
  2. 书籍
  3. 杂志

(使用比较器后) 带类型的商品,最后带ID的商品 ↓

(After the using the comparator) reoders with type and finally with the ID ↓

  1. 书籍
  2. 杂志
  3. 杂志

我的比较器代码:

public Comparator<MyItem> myComparator = (item1, item2) -> {
    //How to add the desc date order with Dates?

    if (item1.typeInt == MyItemModel.TYPE_BOOK && item2.typeInt !=  MyItemModel.TYPE_BOOK) {
        return -1;
    } else if (item1.type !=  MyItemModel.TYPE_BOOK && item2.type ==  MyItemModel.TYPE_BOOK) {
        return 1;
    } else if (item1.type == MyItemModel.TYPE_MAGAZINE && item2.type != MyItemModel.TYPE_MAGAZINE) {
        return -1;
    } else if (item1.type != MyItemModel.TYPE_MAGAZINE && item2.type == MyItemModel.TYPE_MAGAZINE) {
        return 1;
    } 
    return (Integer.compare((int) item1.id, (int) item2.id)) * -1;
};

推荐答案

首先创建一个解析给定日期的函数

First make a function which parse given date

public Date parseDateFromString(String date){
    if(date == null) {
        return null;
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-yy'T'HH:mm:ss", Locale.US);
    try {
        return simpleDateFormat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

然后将其与Comparator进行比较

and then compare it with Comparator like

if (item1.typeInt != item2.typeInt) {
    return item1.typeInt == MyItemModel.TYPE_MAGAZINE ? 1 : -1;
} else {
    if (parseDateFromString(item1.getStartReading()) == null) {
        return 0;
    } else if (parseDateFromString(item2.getStartReading()) == null) {
        return -1;
    } else {
        return parseDateFromString(item2.getStartReading()).after(parseDateFromString(item1.getStartReading())) ? 1 : -1;
    }
}

这篇关于无法在Java/android中的现有比较器中添加对日期进行排序的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆