簡體   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