我正在搜索如何对列表中的元素进行排序,然后通过 getType 获取值 .. 如果 getType == 0 将 y 设置为列表中的第一个元素(但不替换 0 个位置中的项目。)其他人不需要按类型订购它们。我在我的代码中做了两个比较。首先,我通过 getDate 订购了它们……其次,我通过 getType 订购了它们。但当然,我的方法返回的最后一个列表是按类型排序的列表。我不想要那个。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; }谢谢。
2 回答
手掌心
TA贡献1942条经验 获得超3个赞
我假设您想首先按日期对列表进行排序,如果有两个日期相同的项目,您想按类型对它们进行排序。我会这样做:
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/150
提交
取消