Kotlin 2 · Collections and lambdas

Group, sort and count

Group items by a key, sort them and count matches.

val byLength = names.groupBy { it.length }
val sorted = names.sortedByDescending { it.length }
val count = names.count { it.startsWith("G") }

Official Kotlin 2 documentation

More collections and lambdas commands

All collections and lambdas commands · Kotlin 2 cheatsheet

Was this page helpful?