Anonymous user
Sort using a custom comparator: Difference between revisions
→use implicit SAM constructor of interface Comparator in sortedWith parameter
(added Ol) |
|||
Line 2,385:
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
println("Unsorted: $strings")
val sorted = strings.sortedWith
compareValues(b.length, a.length).let {
if (it == 0) compareValues(a.lowercase(), b.lowercase())
else it
}
}
println("Sorted: $sorted")
}</lang>
Line 2,404 ⟶ 2,403:
println("Unsorted: $strings")
val sorted = strings.map { Triple(it, it.length, it.lowercase()) }.sortedWith
compareValues(b.second, a.second).let {
if (it == 0) compareValues(a.third, b.third)
else it
}
}
println("Sorted: $sorted")
}</lang>▼
▲</lang>
|