November 14, 2019
One of the most common ADT that a developer uses in their day-to-day coding is List. And one of the most common operations a developer performs on a list is to order it or sort it with given criteria. In this article, I will focus on sorting a list of objects in Scala.
Mainly, there are two ways of sorting a list in Scala, i.e.
Let’s consider the popular example of sorting IMDB ratings. Below is my IMDB class.
case class ImdbRating(name: String, ratings: Double)
Here is a list of the top five rated movies of all time (source).
val ratings = List( ImdbRating("The Shawshank Redemption", 9.3), ImdbRating("The Godfather ", 9.2), ImdbRating("The Dark Knight", 9.1), ImdbRating("The Godfather: Part II", 9.0), ImdbRating("The Lord of the Rings: The Return of the King", 8.9) )
If you observe closely, the above list is ordered by ratings, but my requirement is to order movies by length of their names.
First, I will try to order the list using
sortWith sorts a given list based on the comparison function that is provided to it. It is a stable sort, which means that an item will not lose its original position if two elements are equal. Here is the code to sort the list by length of names:
val sortedRatings = ratings.sortWith(_.name.size < _.name.size)
If I want to order the list in descending order, then all I need to do is to reverse the
Another way to order the above list is to use
sortBy sorts a given sequence according to the implicitly defined natural
sortWith, this sort is stable as well. Here is the code to order the list.
val sortedRatings = ratings.sortBy(_.name.size)
There is a third way to sort the list of objects that I have not discussed here, and that is to extend the
Ordered trait. The trait forces you to implement the
Ordered trait is somewhat like
Comparable interfaces in java.
If you want more details about sorting or Ordered trait, you can refer to the following videos in which I provide additional examples.