繁体   English   中英

如何按日期排序列表,然后在第一个位置搜索元素和设置

[英]How to order a list by date and then search a element and setting at first position

我正在搜索如何对列表中的元素进行排序,然后通过getType获取值。如果getType == 0,则首先在列表中设置该元素(但不要在0位置替换该项目。)其他则不需要按类型订购它们。

我在代码中完成了两个可比性。 首先,我通过getDate命令。.第二,我通过getType命令。 但是,当然,我的方法返回的最后一个列表是按Type排序的列表。 我不要

public List<PromotionList> returnSortedList(List<PromotionList> inputList) {
        Collections.sort(inputList, (o1, o2) -> {
            if (o1.getDate() != null) {
                SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a", Locale.ENGLISH);
                try {
                    return format.parse(o2.getDate()).compareTo(format.parse(o1.getDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                 //   Log.e("ErrorGetDate", e.getMessage());
                    return 0;
                }
            }

            return 0;
        });

        Collections.sort(inputList, (o1, o2) -> {
            if (o1.getType() != null && o2.getType() != null) {
                if (o1.getPrioridad() == 0) {
                    int prior1 = o1.getType();
                    int prior2 = o2.getType();
                    return Integer.compare(prior1, prior2);
                }
            }
            return 0;

        return inputList;
    }

谢谢。

我假设您想首先按日期对列表进行排序,如果有两个日期相等的项目,则想按类型对它们进行排序。 我会这样做:

Collections.sort(myList, new Comparator<PromotionList>() { 
   public int compare(PromotionList o1, PromotionList o2) { 
     if (o1.getDateTime() == null || o2.getDateTime() == null) return 0; 
     int a = o1.getDateTime().compareTo(o2.getDateTime()); 
     if(a == 0) {
        if (o1.getType() == null || o2.getType() == null) return 0; 
        return o1.getType().compareTo(o2.getType());
     }
     return a;

    } 
  });

如果您只想按日期排序,而不是将所有类型都为0的人放在起始索引处,则可以使用它。 第一种排序方式是根据我复制的与您的代码相同的日期进行排序。

  Collections.sort(myList, new Comparator<PromotionList>() { public int compare(PromotionList o1, PromotionList o2) { if (o1.getDateTime() == null || o2.getDateTime() == null) return 0; return o1.getDateTime().compareTo(o2.getDateTime()); } });

int listSize=myList.getSize();
    for(int i=0;i<listSize;i++){
        if(myList.get(i).getType()==0){
            PromotionList temp=myList.remove(i);
            myList.add(0, temp);
        }
    }

如果您希望类型为“ 0”的字符(在开始时插入)也应按日期排序,然后可以尝试这样做。

Collections.sort(myList, new Comparator<PromotionList>() { public int compare(PromotionList o1, PromotionList o2) { if (o1.getDateTime() == null || o2.getDateTime() == null) return 0; return o1.getDateTime().compareTo(o2.getDateTime()); } });

int insertIndex=0;
    int listSize=myList.getSize();
    for(int i=0;i<listSize;i++){
        if(myList.get(i).getType()==0){
            PromotionList temp=myList.remove(i);
            myList.add(insertIndex, temp);
            insertIndex++;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM