提问人:Shannah 提问时间:7/3/2023 更新时间:7/3/2023 访问量:91
Kotlin 按时间戳和枚举对列表进行排序
Kotlin Sort list by Timestamp and then by enum
问:
我想首先按时间戳(在我的情况下只是一个字符串)对列表进行排序,然后按枚举排序。
仅按我使用的时间戳对列表进行排序
list.sortByDescending { it.timestamp }
或
list.sortWith(compareByDescending<InboxItem> { it.timestamp })
对于枚举,我发现:
list.sortBy{it.enum}
我试图将两者结合起来,但似乎它不会按我的枚举值排序。
list.sortWith(compareByDescending<InboxItem> { it.timestamp }.thenBy{it.enum})
有人可以帮我解决这个问题吗?
答:
1赞
Jorn
7/3/2023
#1
您发布的代码将按照您描述的方式进行排序。请注意,枚举常量的默认排序顺序是按序数(它们在源文件中的顺序)排序,而不是按名称排序。请注意,即使纳秒的差异非常小,时间戳顺序也将始终获胜。
评论