Sorting a List of custom objects using sortWith function in Scala

Hey there!

Welcome to ClearUrDoubt.com.

In the below post, we have seen how to sort List objects using Ordered trait.

Sorting a List of custom objects using Ordered trait in Scala

Today we will look at a Scala program to sort MyEmployee objects based on different fields using the sortWith function defined on List.

sortWith(predicate) function takes a predicate as parameter which tells how to perform sort.

Let’s look at the program:

Output:

In our example, we have used a case class “MyEmployee” with three fields Name, Experience and Salary.

sortWith() function sorts the objects based the predicates provided as above.

Happy Learning :).

Please leave a reply in case of any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *