简体   繁体   English

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

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

I'm searching how to order my elements of a list and then get a value by getType.. if getType == 0 set y that element first at the list (But not replace the item in 0 positions.)the others don't need to order them by Type. 我正在搜索如何对列表中的元素进行排序,然后通过getType获取值。如果getType == 0,则首先在列表中设置该元素(但不要在0位置替换该项目。)其他则不需要按类型订购它们。

I've done two comparables in my code. 我在代码中完成了两个可比性。 First i've order'em by getDate .. second, I've order'em by getType. 首先,我通过getDate命令。.第二,我通过getType命令。 But of course, the last list that my method returns is a list ordered by Type. 但是,当然,我的方法返回的最后一个列表是按Type排序的列表。 I don't want that. 我不要

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;
    }

Thanks. 谢谢。

I assume you want to first sort your list by the date and if there are two items with equal date, you want to want to sort them by type. 我假设您想首先按日期对列表进行排序,如果有两个日期相等的项目,则想按类型对它们进行排序。 I would do it like this: 我会这样做:

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;

    } 
  });

if you want to only sort by date and than put all those who has type as 0 at starting index then you can use this. 如果您只想按日期排序,而不是将所有类型都为0的人放在起始索引处,则可以使用它。 First sort is for sorting on basis of date which i copied same as your code. 第一种排序方式是根据我复制的与您的代码相同的日期进行排序。

  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);
        }
    }

If you want those which has type as "0"(which are inserted at starting) should also be sorted by date than you can try this. 如果您希望类型为“ 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.

相关问题 如何声明元素在列表中的位置(确认它是列表中的第一个元素) - How to assert position of element on the list (Confirm it is first element on the list) 如何使用二进制搜索来查找具有一定权重的数组中的第一个元素(另一个数组中的另一个元素)? - How to use binary search in order to find the first element in an array that has certain weight (another element in a different array)? 对元素仍处于第一个位置的列表进行排序 - Sort a list with element still in first position 使用 Stream 将列表中的元素移动到第一个 position - Move an element in the list to the first position using Stream 如何搜索列表中的元素? - How to search element in a List? 将 Map 的第一个元素添加到第一个 Position 的另一个列表中,依此类推...第二个 map 元素到第二个 Z4757FE07FD492ADBEEAZ6 列表中 - Add first element of Map into another list at 1st Position and so on…second map element into second position of list 如何在保留元素顺序的同时将列表的最小值移动到第一个元素 - How to moves the minimum value of the list to be the first element while preserving the order of the elements 在第一个 position 插入元素时返回不正确的链接列表 - Incorrect Linked List being returned when inserting element at first position 如何将List中的位置绑定到该元素的属性? - How to bind the position in a List to an attribute of that element? 如何在Java中按日期/时间比较和排序列表? - How to compare and order list by date/time in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM