Scala Tutorial - Learn How To Use SortWith Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:09 pm

Overview

In this tutorial, we will learn how to use the sortWith function with examples on collection data structures in Scala. The sortWith function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The sortWith method takes a predicate function and will use it to create a new collection where the elements are sorted by the predicate function.

 

As per the Scala documentation, the definition of the sortWith method is as follows:

def sortWith(lt: (A, A) ⇒ Boolean): Repr

The sortWith method is a member of SeqLike trait.

Steps

1. How to create a case class to represent Donut objects

The code below shows to create a case class to represent donut objects.


println("\nStep 1: How to create a case class to represent Donut objects")
case class Donut(name: String, price: Double)

 

2. How to create a Sequence of type Donut

Using the donut case class from Step 1, we can now initialize a Sequence of donut elements as shown below.


println("\nStep 2: How to create a Sequence of type Donut")
val donuts: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))
println(s"Elements of donuts = $donuts")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to create a Sequence of type Donut
Elements of donuts2 = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))

 

3. How to sort a sequence of case class objects using the sortWith function

The code below shows how to use the sortWith method to sort each donut element in the sequence by their price property.


println("\nStep 3: How to sort a sequence of case class objects using the sortWith function")
println(s"Sort a sequence of case classes of type Donut, sorted with price = ${donuts.sortWith(_.price < _.price)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to sort a sequence of case class objects using the sortWith function
Sort a sequence of case classes of type Donut, sorted with price = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))

 

4. How to sort a sequence of case class objects in ascending order using the sortWith function

Building on the example from Step 3, the code below also demonstrates how to use the sortWith method explicitly by passing in a predicate function to sort each element in the donut sequence in ascending order.


println("\nStep 4: How to sort a sequence of case class objects in ascending order using the sortWith function")
println(s"Sort a sequence of case classes of type Donut, sorted with price in ascending order = ${donuts.sortWith(_.price < _.price)}")
println(s"Sort a sequence of case classes of type Donut, sorted with price in ascending order explicitly = ${donuts.sortWith((d1,d2) => d1.price < d2.price)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to sort a sequence of case class objects in ascending order using the sortWith function
Sort a sequence of case classes of type Donut, sorted with price in ascending order = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))
Sort a sequence of case classes of type Donut, sorted with price in ascending order explicitly = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))

5. How to sort a sequence of case class objects in descending order using the sortWith function

Similar to Step 4, the code below shows how to use the sortWith method explicitly using a predicate function to sort each element in the donut sequence in descending order.


println("\nStep 5: How to sort a sequence of case class objects in descending order using the sortWith function")
println(s"Sort a sequence of case classes of type Donut, sorted with price in descending order = ${donuts.sortWith(_.price > _.price)}")
println(s"Sort a sequence of case classes of type Donut, sorted with price in descending order explicitly = ${donuts.sortWith((d1,d2) => d1.price > d2.price)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to sort a sequence of case class objects in descending order using the sortWith function
Sort a sequence of case classes of type Donut, sorted with price in descending order = List(Donut(Glazed Donut,2.5), Donut(Strawberry Donut,2.0), Donut(Plain Donut,1.5))
Sort a sequence of case classes of type Donut, sorted with price in descending order explicitly = List(Donut(Glazed Donut,2.5), Donut(Strawberry Donut,2.0), Donut(Plain Donut,1.5))

This concludes our tutorial on Learn How To Use SortWith Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to create a case class to represent Donut objects
  • How to create a Sequence of type Donut
  • How to sort a sequence of case class objects using the sortWith function
  • How to sort a sequence of case class objects in ascending order using the sortWith function
  • How to sort a sequence of case class objects in descending order using the sortWith function

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use the tail function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: