I want to sort list descending based on UTC DateTime which is in String form.
My Class
data class CartEntity( val itemId: String, var itemName: String, var createdDate: String)
in this createdDate is "2020-07-28T14:28:52.877Z"
What I have tried
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)
}
}
})
But still sort by descending is not working as expected