“Sortuj niestandardową listę obiektów w Kotlin” Kod odpowiedzi

Sortuj niestandardową listę obiektów w 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 })
Coder Thirteen

Sortuj listę obiektów 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) }
Exuberant Elk

Sortuj listę obiektów 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) }
Exuberant Elk

Sortuj listę obiektów 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) }
Exuberant Elk

Odpowiedzi podobne do “Sortuj niestandardową listę obiektów w Kotlin”

Pytania podobne do “Sortuj niestandardową listę obiektów w Kotlin”

Więcej pokrewnych odpowiedzi na “Sortuj niestandardową listę obiektów w Kotlin” w Kotlin

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu