gpt4 book ai didi

java - 如何按长字段对 List 进行排序
转载 作者:行者123 更新时间:2023-11-29 04:21:03 32 4
gpt4 key购买 nike

List<DetailsDescription> result = new ArrayList<>();
result.add(
new DetailsDescription(((Date) row.getObject("alarm_start_timestamp")).getTime()));

result.stream().sorted(Comparator.comparing(DetailsDescription::getStartTimestamp)
.reversed())
.limit(PAGE_SIZE)
.collect(Collectors.toList());

大家好!

我的列表中有几行,并且有我想要排序的时间戳。问题是比较器需要一个 int 并且我不能强制转换 long (即日期),因为如果我这样做,我会削减一些最后的数字。代码可以工作,但排序不准确(它删除了最后一位数字)。

它可能会帮助你理解:

result.stream().sorted((a,b)->a.getStartTimestamp() - b.getStartTimestamp()).collect(Collectors.toList());

错误:

Type mismatch: cannot convert from long to int

最佳答案

利用Comparator.comparingLong() :

result.stream().sorted(Comparator.comparingLong(DetailsDescription:: getStartTimestamp)).collect(Collectors.toList());

上面的代码可以工作。

关于java - 如何按长字段对 List<Object> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171373/

32 4 0