DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How to Submit a Post to DZone
  • How to LINQ Between Java and SQL With JPAStreamer
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Auditing Tools for Kubernetes

Trending

  • How to Submit a Post to DZone
  • How to LINQ Between Java and SQL With JPAStreamer
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Auditing Tools for Kubernetes
  1. DZone
  2. Coding
  3. Languages
  4. Scala: Sorting Lists of Objects

Scala: Sorting Lists of Objects

See how to best sort lists in Scala.

Gaurav Gaur user avatar by
Gaurav Gaur
CORE ·
Nov. 14, 19 · Tutorial
Like (5)
Save
Tweet
Share
14.51K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

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.

  •  sortWith.

  •  sortBy.

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.

You may also like: Scala Maps and Sorting.

sortWith

First, I will try to order the list using sortWith. 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 < operator. 

sortBy

Another way to order the above list is to use sortBy. sortBy sorts a given sequence according to the implicitly defined natural Ordering. Like 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 compare method. 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.

https://youtu.be/_ScnsjXQZwQ.

https://youtu.be/zOvLTu2ZOxw.


Further Reading

  • A Journey With Scala.
  • 10 Reasons to Learn Scala and FP.
  • Clean Code Best Practices in Scala.
Scala (programming language) Sorting Object (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • How to Submit a Post to DZone
  • How to LINQ Between Java and SQL With JPAStreamer
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Auditing Tools for Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: