[英]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.