How to Sort a list in Scala?
Last Updated :
29 Apr, 2024
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.
Using the sorted Method:
Scala's sorted method allows you to sort a list of elements in ascending order. This method returns a new sorted list without modifying the original list.
Scala
object Main {
def main(args: Array[String]): Unit = {
val numbers = List(5, 3, 8, 1, 2, 4, 7, 6)
val sortedNumbers = numbers.sorted
println(numbers)
println(sortedNumbers) // Output: List(1, 2, 3, 4, 5, 6, 7, 8)
}
}
OutputUsing the sortBy Method:
The sortBy method allows you to specify a function that extracts a key from each element, and then sorts the elements based on the keys in ascending order.
Scala
object Main {
def main(args: Array[String]) {
val fruits = List("apple", "banana", "orange", "grape")
val sortedFruits = fruits.sortBy(_.length)
println(sortedFruits) // Output: List("apple", "grape", "banana", "orange")
}
}
OutputUsing the sortWith Method:
The sortWith method allows you to provide a comparison function that determines the order of elements in the sorted list.
Scala
object Main {
def main(args: Array[String]) {
val numbers = List(5, 3, 8, 1, 2, 4, 7, 6)
val sortedNumbers = numbers.sortWith(_ < _)
println(sortedNumbers) // Output: List(1, 2, 3, 4, 5, 6, 7, 8)
}
}
OutputSorting in Descending Order:
To sort a list in descending order, you can use the reverse method after sorting in ascending order.
Scala
object Main {
def main(args: Array[String]) {
val numbers = List(5, 3, 8, 1, 2, 4, 7, 6)
val sortedNumbersDesc = numbers.sorted.reverse
println(sortedNumbersDesc) // Output: List(8, 7, 6, 5, 4, 3, 2, 1)
}
}
OutputConclusion:
Sorting lists in Scala is straightforward, thanks to the built-in methods like sorted, sortBy, and sortWith. These methods provide flexibility in sorting lists based on different criteria and in different orders. By understanding these methods, you can efficiently sort lists in your Scala programs.
With these examples and explanations, you should now have a solid understanding of how to sort lists in Scala. Whether you're working with numbers, strings, or custom objects, Scala's rich collection library provides powerful tools for sorting data efficiently.
Similar Reads
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 reverse a list in scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. We use reverse function to reverse a list in Scala. Below are the examples to reverse a list. Reverse a simple list scala // Scala program to reverse a simp
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
How to sort a Scala Map by key 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 //
2 min read
How to access list elements in Scala? In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possibl
2 min read