In Scala, there are several ways to sort an array. Here are a few examples with explanations:
1. Using the sorted method: This method is available on any Scala collection, including arrays. It sorts the elements in ascending order.
val arr = Array(3, 1, 4, 1, 5)
val sortedArr = arr.sorted // returns Array(1, 1, 3, 4, 5)
2. Using the sortWith method: This method takes a function that compares two elements and returns a boolean. If the boolean is true, the elements are swapped. This allows you to specify a custom sorting order.
val arr = Array("apple", "banana", "orange", "pear")
val sortedArr = arr.sortWith((a, b) => a < b) // returns Array("apple", "banana", "orange", "pear")
val reverseSortedArr = arr.sortWith((a, b) => a > b) // returns Array("pear", "orange", "banana", "apple")
3. Using the sortBy method: This method takes a function that maps each element to a value that will be used for sorting. If the resulting values are comparable, the elements will be sorted accordingly.
case class Person(name: String, age: Int)
val people = Array(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20))
val sortedPeople = people.sortBy(_.age) // returns Array(Person("Charlie", 20), Person("Alice", 25), Person("Bob", 30))
val reverseSortedPeople = people.sortBy(_.age)(Ordering[Int].reverse) // returns Array(Person("Bob", 30), Person("Alice", 25), Person("Charlie", 20))
In all of these examples, the original array is not modified. Instead, a new sorted array is returned.