“Sortieren Sie die Objektliste Kotlin” Code-Antworten

Sortieren Sie die Objektliste 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

Sortieren Sie die Objektliste 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

Sortieren Sie die Objektliste 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

Ähnliche Antworten wie “Sortieren Sie die Objektliste Kotlin”

Fragen ähnlich wie “Sortieren Sie die Objektliste Kotlin”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen