Answers for "sort object list kotlin"

2

sort object list kotlin

val dates = mutableListOf(
	Date(2020, 4, 3),
	Date(2021, 5, 16),
	Date(2020, 1, 29)
)

println("--- ASC ---")
dates.sortWith(compareBy<Date> { it.year }.thenBy { it.month }.thenBy { it.day })
dates.forEach { println(it) }
Posted by: Guest on October-21-2020
1

sort object list kotlin

val dates = mutableListOf(
	Date(2020, 4, 3),
	Date(2021, 5, 16),
	Date(2020, 1, 29)
)

println("--- ASC ---")
val sortedDates = dates.sortedWith(compareBy<Date> { it.year }.thenBy { it.month }.thenBy { it.day })
dates.forEach { println(it) }
println("------")
sortedDates.forEach { println(it) }
Posted by: Guest on October-21-2020
0

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) }
Posted by: Guest on October-21-2020

Browse Popular Code Answers by Language