Last active
April 9, 2020 16:07
-
-
Save pavelgordon/3bf6ccc5fae38d81097f76deb3b01831 to your computer and use it in GitHub Desktop.
Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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