Sort using a custom comparator: Difference between revisions

(added Ol)
Line 2,385:
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
println("Unsorted: $strings")
 
val sorted = strings.sortedWith ({ a, b ->
kotlin.Comparator { a, b ->
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 ({ a, b ->
kotlin.Comparator { a, b ->
compareValues(b.second, a.second).let {
if (it == 0) compareValues(a.third, b.third)
else it
}
}).map { it.first }
 
println("Sorted: $sorted")
}</lang>
}
</lang>
 
 
Anonymous user