gpt4 book ai didi

sorting - 按降序对 @Sortable 对象列表进行排序

转载 作者:行者123 更新时间:2023-12-02 21:20:58 25 4
gpt4 key购买 nike

我有课

@Sortable(includes = ['date'])
class Item {
// other fields not relevant to this question
Date date
}

如果我对这些对象的List进行排序,它将根据date字段按升序对它们进行排序。有没有办法按降序对它们进行排序?我知道我可以对升序排序的结果调用 reverse() ,但这似乎有点低效

最佳答案

这里有几种方法:

def items = [
new Item(date: new Date(40000)),
new Item(date: new Date(1000)),
new Item(date: new Date(200000)),
new Item(date: new Date(00100)),
]

items.sort { a, b -> b <=> a }

items.sort(true, Collections.reverseOrder())

关于sorting - 按降序对 @Sortable 对象列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30258367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com