我想根据字符串形式的 UTC 日期时间对列表进行降序排序。
我的课
data class CartEntity( val itemId: String, var itemName: String, var createdDate: String)
Run Code Online (Sandbox Code Playgroud)
在这个createdDate中是“2020-07-28T14:28:52.877Z”
我尝试过的
const val UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
Collections.sort(list, object : Comparator<CartEntity> {
var f: DateFormat =
SimpleDateFormat(
AppConstants.UTC_FORMAT, Locale.ENGLISH
)
override fun compare(o1: CartEntity?, o2: CartEntity?): Int {
return try {
val firstitem = f.parse(o1?.createdDate!!)
val seconditem = f.parse(o2?.createdDate!!)
firstitem.compareTo(seconditem)
} catch (e: ParseException) {
throw IllegalArgumentException(e)
}
}
})
Run Code Online (Sandbox Code Playgroud)
但仍按降序排序未按预期工作
kotlin ×1