0

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.

I've done two comparables in my code. First i've order'em by getDate .. second, I've order'em by getType. But of course, the last list that my method returns is a list ordered by 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.

  • A 2 field compare can be done something like `Comparator comparator = Comparator.comparing(PromotionList::getDateTime).thenComparing(PromotionList::getType);`. because you may have nulls, you may want to implement custom null safety comparator as well - https://stackoverflow.com/a/23908426/4252352 – Mark Sep 06 '18 at 13:06

2 Answers2

0

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;

    } 
  });
lkslt
  • 11
  • 1
0

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. 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.

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++;
        }
    }
Nawnit Sen
  • 973
  • 2
  • 7
  • 14