kotlin sort array of objects by property code example
Example 1: sort custom object list in kotlin
val messages = mutableListOf<Message>(
Message(from = "Tonnie", messageString = "Hi", timestamp = Date()),
Message(from = "Victoria", messageString = "Miss You", timestamp = Date()))
messages.sortWith(compareBy { it.timestamp })
Example 2: sort object list kotlin
println("--- DESC ---")
val sortedDatesDescending =
dates.sortedWith(compareBy<Date> { it.year }.thenBy { it.month }.thenBy { it.day }).reversed()
dates.forEach { println(it) }
println("------")
sortedDatesDescending.forEach { println(it) }