How to sort a Scala Map by key Last Updated : 13 Aug, 2019 Comments Improve Suggest changes Like Article Like Report Map is same as dictionary which holds key:value pairs. In this article, we will learn how to sort a Scala Map by key. We can sort the map by key, from low to high or high to low, using sortBy. Syntax : mapName.toSeq.sortBy(_._1):_* Let's try to understand it with better example. Example #1: Scala // Scala program to sort given map by key 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 by key val res = ListMap(mapIm.toSeq.sortBy(_._1):_*) println(res) } } Output: Map(Charlie -> 50, Jhavesh -> 20, Zash -> 30) Example #2: Scala // Scala program to sort given map by key 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) // reverse map in ascending order val res = ListMap(mapIm.toSeq.sortWith(_._1 < _._1):_*) println(res) } } Output: Map(Charlie -> 50, Jhavesh -> 20, Zash -> 30) Example #3: Scala // Scala program to sort given map by key 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) // reverse map in descending order val res = ListMap(mapIm.toSeq.sortWith(_._1 > _._1):_*) println(res) } } Output: Map(Zash -> 30, Jhavesh -> 20, Charlie -> 50) Comment More infoAdvertise with us Next Article How to sort a Scala Map by key S Shivam_k Follow Improve Article Tags : Python Scala Scala Scala-Method Scala-Map +1 More Practice Tags : python Similar Reads 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: Scala // Scala program to sort given map by value impor 2 min read How to Sort an Array in Scala? Sorting arrays effectively is essential for many applications, regardless of whether you're working with texts, custom objects, or numerical data. Because Scala is a strong and expressive language, it provides a variety of array sorting methods that may be customized to fit various needs and situati 5 min read How to Sort a list in Scala? Sorting a list is a common operation in programming, and Scala provides convenient ways to accomplish this task. In this article, we'll explore different methods to sort a list in Scala, along with examples. Table of Content Using the sorted Method:Using the sortBy Method:Using the sortWith Method:S 2 min read How to get all the keys from a Scala map In order to get all the keys from a Scala map, we need to use either keySet method (to get all the keys as a set) or we can use keys method and if you want to get the keys as an iterator, you need to use keysIterator method. Now, lets check some examples. Example #1: Scala // Scala program of keySet 2 min read How to Use Sortby in Scala? In this article, we will learn to use the sortBy function in Scala. The sortBy function is used to sort elements in a collection based on specified sorting criteria. Table of Content Using SortBy for Sorting an ArrayUsing SortBy for Sorting an Array in Reverse Order Using SortBy for Sorting By the S 4 min read Like