Sort using a custom comparator: Difference between revisions
Content added Content deleted
Line 2,263: | Line 2,263: | ||
<lang kotlin>fun main(args: Array<String>) { |
<lang kotlin>fun main(args: Array<String>) { |
||
val strings = listOf("Here", "are", "some", "sample", "strings", "to", "be", "sorted") |
|||
println("Unsorted: $strings") |
|||
val sorted = strings.map { Triple (it, it.length, it.lowercase()) }.sortedWith ( |
|||
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> |
|||
{{out}} |
{{out}} |
||
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted] |
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted] |
||
Sorted: |
Sorted: [strings, sample, sorted, Here, some, are, be, to]</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |