Sorting Arrays of Objects

You can use the sortBy() method in conjunction with a custom comparison function that takes an object’s key and uses it to sort arrays of custom objects.

Below is the Scala program to implement the approach:

Scala
case class Person(name: String, age: Int)
val people = Array(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20))
val sortedPeople = people.sortBy(_.age) // Sort by age
println(sortedPeople.mkString(", ")) // Output: Person(Charlie,20), Person(Alice,25), Person(Bob,30)

Output:

Sorting Arrays of Objects

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 situations.

This article will examine the nuances of sorting arrays in Scala and the different approaches and techniques used to do this task successfully.

Table of Content

  • What is Sorting in an Array?
  • Using ‘sortWith’ Method
  • Using ‘sorted()’ Method
  • Using ‘sortBy()’ Method
  • Sorting Arrays of Objects
  • Sorting Arrays in Reverse Order
  • Conclusion
  • FAQs

Similar Reads

What is Sorting in an Array?

Rearranging an array’s contents in a certain order, usually according to some sort of criterion (e.g., numerical value, alphabetical order, or custom comparison function), is known as sorting....

Using ‘sortWith’ Method

You can specify your own custom sorting logic using the ‘sortWith()’ method. It requires a function to compare two elements, and it outputs a boolean representing the order of sorting....

Using ‘sorted()’ Method

The ‘sorted()’ method returns a new sorted array without altering the original array, thus you can use it if you would rather not change the original one....

Using ‘sortBy()’ Method

Sorting can be done using a custom comparison function with the help of the ‘sortBy()’ method in Scala’s collections package. Using a function that associates array members with keys, this technique sorts the array according to these keys....

Sorting Arrays of Objects

You can use the sortBy() method in conjunction with a custom comparison function that takes an object’s key and uses it to sort arrays of custom objects....

Sorting Arrays in Reverse Order

After sorting an array using one of the above ways, you may use the ‘reverse’ method to sort the array in the opposite order....

Conclusion

Because Scala includes built-in methods, sorting arrays in the language is simple. Scala offers versatile options to match your needs, whether you need to sort custom objects, arrays of primitive types, or custom sorting criteria. You may easily manage and modify arrays in your Scala applications by utilizing these techniques....

FAQs based on How to Sort an Array in Scala?

1. Why does ‘sort()’ not work with arrays of primitive types in Scala ?...

Contact Us