Skip to content

Instantly share code, notes, and snippets.

@pavelgordon
Last active April 9, 2020 16:07
Show Gist options
  • Save pavelgordon/3bf6ccc5fae38d81097f76deb3b01831 to your computer and use it in GitHub Desktop.
Save pavelgordon/3bf6ccc5fae38d81097f76deb3b01831 to your computer and use it in GitHub Desktop.
Sorting
fun main() {
val s = "beans onion corn onion carrot potato avocado avocado carrot"
.split(" ")
.groupingBy { it }
.eachCount()
.toList()
.sortedWith(compareBy<Pair<String, Int>> { -it.second }.thenBy { it.first })
// .sortedWith(compareBy({ -it.second }, { it.first })) // also possible
.forEach { (k, v) ->
println("$v x $k")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment