How to sort a kotlin map by value

How to sort a map by value in Kotlin:

Kotlin map is used to hold key-value pairs and using a key, we can access the value that is associated with it. The keys of a Kotlin map should be unique. We can’t have duplicate keys, but different keys can have the same value. In this post, we will learn how to sort a Kotlin map by its values.

The program will create one map with some key-value pairs and it will sort them by their values.

Method 1: sort a map by values by converting it to a list:

With this approach, we will:

  • Convert the map to a list.
  • sort the list.
  • Convert the list back to a map.

Below is the complete program:

fun main() {
    val givenMap = hashMapOf<String, Int>()
    givenMap["one"] = 1
    givenMap["two"] = 2
    givenMap["three"] = 3
    givenMap["four"] = 4
    givenMap["five"] = 5
    givenMap["six"] = 6

    println("Given map :")
    givenMap.forEach { (k, v) -> println("$k => $v") }

    val sortedMap = givenMap.toList().sortedBy { (k, v) -> v }.toMap()

    println("Sorted map :")
    sortedMap.forEach { (k, v) -> println("$k => $v") }
}

Download it on GitHub

Here, we have created one hashMap with keys of type String and values of type Int. We inserted a couple of key-value pairs to this map and these key-value pairs are printed using a forEach loop.

The program sorted the map by changing it to a list by using the sortedBy method. The sorted list is converted back to a map using the toMap() method.

Output:

This program prints the below output:

Given map :
six => 6
four => 4
one => 1
two => 2
three => 3
five => 5
Sorted map :
one => 1
two => 2
three => 3
four => 4
five => 5
six => 6

Kotlin sort map by values

Method 2: By using sortedBy on entries of the map:

We can use the sortedBy method on the entries of the given map. It will sort the pairs based on their values if we pass it.value as a parameter to the sortedBy method. These values can be put in a LinkedHashMap with the help of a forEach loop:

fun main() {
    val givenMap = hashMapOf<String, Int>()
    givenMap["one"] = 1
    givenMap["two"] = 2
    givenMap["three"] = 3
    givenMap["four"] = 4
    givenMap["five"] = 5
    givenMap["six"] = 6

    println("Given map :")
    givenMap.forEach { (k, v) -> println("$k => $v") }

    var sortedMap: MutableMap<String,Int> = LinkedHashMap()

    givenMap.entries.sortedBy { it.value }.forEach{sortedMap[it.key] = it.value}

    println("Sorted map :")
    sortedMap.forEach { (k, v) -> println("$k => $v") }
}

Download it on GitHub

It will print similar output:

Given map :
six => 6
four => 4
one => 1
two => 2
three => 3
five => 5
Sorted map :
one => 1
two => 2
three => 3
four => 4
five => 5
six => 6

Method 3: By using the entries.sortedWith method:

This is almost similar to the previous example. The only difference is that we are using the sortedWith method to sort the items. We need to pass java.util.Map.Entry.comparingByValue() as the parameter.

fun main() {
    val givenMap = hashMapOf<String, Int>()
    givenMap["one"] = 1
    givenMap["two"] = 2
    givenMap["three"] = 3
    givenMap["four"] = 4
    givenMap["five"] = 5
    givenMap["six"] = 6

    println("Given map :")
    givenMap.forEach { (k, v) -> println("$k => $v") }

    var sortedMap: MutableMap<String,Int> = LinkedHashMap()

    givenMap.entries.sortedWith(java.util.Map.Entry.comparingByValue()).forEach{sortedMap[it.key] = it.value}

    println("Sorted map :")
    sortedMap.forEach { (k, v) -> println("$k => $v") }
}

Download it on GitHub

Output:

Given map :
six => 6
four => 4
one => 1
two => 2
three => 3
five => 5
Sorted map :
one => 1
two => 2
three => 3
four => 4
five => 5
six => 6

You might also like: