Decorate-sort-undecorate idiom: Difference between revisions

no edit summary
No edit summary
Line 594:
"chrestomathy"
</pre>
 
=={{header|Kotlin}}==
Kotlin already has a `sortedWith` function that takes a custom comparator, so there is no need to write such a function; however, one is shown here as a code example.
<syntaxhighlight lang="kotlin">
fun main() {
val list = listOf("Rosetta", "Code", "is", "a", "programming", "chrestomathy", "site")
println(sorted(list, String::length))
}
 
fun <T, C: Comparable<C>> sorted(list: Collection<T>, keyFn: (T) -> C): List<T> =
list
.map { it to keyFn(it) }
.sortedBy { it.second }
.map { it.first }
</syntaxhighlight>
 
=={{header|Lua}}==
47

edits