How to sort a Scala Map by value

In this article, we will learn how to sort a Scala Map by value. We can sort the map by key, from low to high or high to low, using sortBy method.
Syntax:

MapName.toSeq.sortBy(_._2):_*

Let’s try to understand it with better example.

Example #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to sort given map by value
import scala.collection.immutable.ListMap
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a map 
        val mapIm = Map("Zash" -> 30
                        "Jhavesh" -> 20
                        "Charlie" -> 50
          
          
        // Sort the map by value
        val res = ListMap(mapIm.toSeq.sortBy(_._2):_*)
        println(res)
    

chevron_right


Output:

Map(Jhavesh -> 20, Zash -> 30, Charlie -> 50)

In above example, we can see mapIm.toSeq.sortBy method is used to sort the map by key values.

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to sort given map by value
// in ascending order
import scala.collection.immutable.ListMap
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a map 
        val mapIm = Map("Zash" -> 30
                        "Jhavesh" -> 20
                        "Charlie" -> 50
          
        // Sort map value in ascending order
        val res = ListMap(mapIm.toSeq.sortWith(_._2 < _._2):_*)
        println(res)
    

chevron_right


Output:

Map(Jhavesh -> 20, Zash -> 30, Charlie -> 50)

Here, In above example we are sorting map in ascending order by using mapIm.toSeq.sortWith(_._2 < _._2):_*
Example #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to sort given map by value
// in decending order
import scala.collection.immutable.ListMap
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a map 
        val mapIm = Map("Zash" -> 30
                        "Jhavesh" -> 20
                        "Charlie" -> 50
          
        // Sort map value in descending order
        val res = ListMap(mapIm.toSeq.sortWith(_._2 > _._2):_*)
        println(res)
    

chevron_right


Output:

Map(Charlie -> 50, Zash -> 30, Jhavesh -> 20)

Above example show to sorting map in descending order.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.