KotlinCollection OpsgroupBy
fold

groupByCollection Ops

sortedBy

Groups elements of the original collection by the key returned by the given selector.

Syntax

collection.groupBy { keySelector }

Example

Enter values below to update the example in real time.

val
words
listOf
grouped
groupBy
it
println
apple
ant
banana
cherry
cat
val words = listOf("apple", "ant", "banana", "cherry", "cat")
val grouped = words.groupBy { it.first() }
println(grouped) // {a=[apple, ant], b=[banana], c=[cherry, cat]}