6 ms·
The Kotlin reference is not supposed to be used for finding methods. The way to go is to use auto completion in an IDE. If you type `myArray.sort<ctrl+space>`,
by voddan 10y ago
The Kotlin reference is not supposed to be used for finding methods.
The way to go is to use auto completion in an IDE. If you type `myArray.sort<ctrl+space>`, you get all one needs, including `sort`, `sorted`, `sortBy` (which you needed).
Alternatively you could search the reference page for Array or MutableList or MutableMap, not sure what you wanted:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/ https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/ https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collecti...
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/ https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collecti...
- Scene_Cast2 10y agoI had something like this: var heights: Array< Pair<Int, Int> > = ... //populating the array heights.sortBy { it.second }; It was not obvious at all that I had to use curly braces. The documentation says: inline fun <T, R : Comparable<R>> MutableList<T>.sortBy( crossinline selector: (T) -> R?) (source) This is after I figured out that I'm not supposed to use sortWith (I never figured out how to use this one)
- voddan 10y agoThe curly braces are the syntax for lambdas, one of the basic types `(T) -> R?`. There are plenty of examples for using that, but in the respective chapters: https://kotlinlang.org/docs/reference/lambdas.html#higher-order-functions https://kotlinlang.org/docs/reference/lambdas.html#higher-or... or in this bunch of examples: https://kotlinlang.org/docs/reference/idioms.html#filtering-a-list https://kotlinlang.org/docs/reference/idioms.html#filtering-... The docs are arranged in a way that one reads them in the order they are written (at least the first half). I supposed that would take more than half an hour given at the competition. I don't think Kotlin is the best choice for programming challenges like codeforces - they really don't let him shine (although I use it whenever I can). But if this is a valid case, we can collaborate and write a short tutorial for C/C++ to Kotlin programmers.
- Scene_Cast2 10y agoOops, copied the wrong snippet out of the two: inline fun <T, R : Comparable<R>> Array<out T>.sortBy( crossinline selector: (T) -> R?) (source)