Decorate-sort-undecorate idiom: Difference between revisions
Content added Content deleted
Line 596: | Line 596: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Kotlin already has a `sortedBy` function that can sort using a key extractor directly, so there is no need to decorate/undecorate. Nevertheless, the code example below shows how to decorate, sort and undecorate. |
|||
<syntaxhighlight lang="kotlin"> |
<syntaxhighlight lang="kotlin"> |
||
fun main() { |
fun main() { |
||
val list = listOf("Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site") |
val list = listOf("Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site") |
||
println |
println(list.sortedBySchwartzian(String::length)) |
||
} |
} |
||
/** |
|||
⚫ | |||
* Returns a sorted list using the Schwartzian Transform which guarantees minimal use of the |
|||
list |
|||
* key extractor function. Use when the key extractor function is an expensive operation. |
|||
⚫ | |||
*/ |
|||
⚫ | |||
⚫ | |||
.sortedBy { it.second } |
.sortedBy { it.second } |
||
.map { it.first } |
.map { it.first } |