Scala Tutorial - Learn How To Use Sorted Function With Examples

By Nadim Bahadoor | Last updated: December 12, 2017 at 21:05 pm

Overview

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

 

The sorted method will return a new collection with elements sorted by their natural order.

 

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

def sorted[B >: A](implicit ord: math.Ordering[B]): Repr

The sorted method is a member of SeqLike trait.

Steps

1. How to initialize donut prices

The code below shows to initialize a Sequence of type Double to represent donut prices.


println("Step 1: How to initialize donut prices")
val prices: Seq[Double] = Seq(1.50, 2.0, 2.50)
println(s"Elements of prices = $prices")

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


Step 1: How to initialize donut prices
Elements of prices = List(1.5, 2.0, 2.5)

 

2. How to sort a sequence of type Double using the sorted function

The donut prices Sequence from Step 1 can be sorted by the natural order of type Double by using the sorted method.


println("\nStep 2: How to sort a sequence of type Double using the sorted function")
println(s"Sort a sequence of type Double by their natural ordering = ${prices.sorted}")

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


Step 2: How to sort a sequence of type Double using the sorted function
Sort a sequence of type Double by their natural ordering = List(1.5, 2.0, 2.5)

 

3. How to initialize a Sequence of donuts

The code below shows how to define a Sequence of type String to represent donut elements.


println("\nStep 3: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

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


Step 3: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

4. How to sort a sequence of type String using the sorted function

The donut sequence of type String from Step 3 can be sorted by their natural ordering using the sorted method.


println println("\nStep 4: How to sort a sequence of type String using the sorted function")
println(s"Sort a sequence of type String by their natural ordering = ${donuts.sorted}")

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


Step 4: How to sort a sequence of type String using the sorted function
Sort a sequence of type String by their natural ordering = List(Glazed Donut, Plain Donut, Strawberry Donut)

This concludes our tutorial on Learn How To Use Sorted 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 initialize donut prices
  • How to sort a sequence of type Double using the sorted function
  • How to initialize a Sequence of donuts
  • How to sort a sequence of type String using the sorted 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 sortWith function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Principal Engineer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on