Sorting in scala using sorted,sortBy and sortWith function

Reading Time: 4 minutes

Sorting is arranging the data in ascending or descending order. Sorted data helps us searching easily. In mobile phone contacts are sorted in the alphabetic order it helps us to find easily a mobile number of any people.
Scala usesTimSort, which is a hybrid of Merge Sort and Insertion Sort.
Here is three sorting method of Scala.


Here is signature

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

The sorted function is used to sort the sequence in Scala like (List, Array, Vector, Seq). The sorted function returns new Collection which is sorted by their natural order.

Now, Here is a small example Sorted with Seq

If you want to sort in descending order then, use this signature   Seq.sorted(Ordering.DataType.reverse)

If you want to sort on the basis of an attribute of a case class using  sorted method, then you need to extend Ordered trait and override abstract method compare. In compare method, we define on which attribute we want to sort the objects of  case class.
here is an example
sort on the basis of the name attribute of the case class.

If you do not extend Ordered trait and want to sort a case class attribute then the compiler does not know in which attribute basis it will sort so it will give a compile-time error.
here is an example.


Here is signature

def sortBy[B](f: A => B)(implicit ord: Ordering[B]): Repr 

The sortBy function is used to sort one or more attributes.
Here is a small example.
 sort based on a single attribute of the case class.

sort in descending  by salary

sort is based on multiple attributes, it will sort based on the first attribute if more than one value in the first attribute is same then it will sort on the basis of the second attribute and so on.

sort list of a tuple by their second element using sortBy

similarly, we can  sort list of a tuple by their first element


Here is signature

 def sortWith(lt: (A, A) => Boolean): Repr 

The sortWith function Sorts this sequence according to a comparison function. it takes a comparator function and sort according to can provide your own custom comparison function.

Here is a small example

you can also sort by using own function.

Please comment, if you have any doubt or suggestion.
thank you 🙂


5 thoughts on “Sorting in scala using sorted,sortBy and sortWith function5 min read

  1. Good explanation buddy. To compare, java Comparable and Comparator and Sorted, SortBy, SortWith…

    SortWith is just like Comparator in this case AgeComparator or SalaryComparator etc… remaining to are Sorted and SortBy looks like Comparable(Ordering in case of scala)

  2. According to Java 7 API docs

    Arrays#Sort() for object arrays now uses TimSort, which is a hybrid of MergeSort and InsertionSort.

    On the other hand, Arrays#sort() for primitive arrays now uses Dual-PivotQuickSort.

Comments are closed.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!

%d bloggers like this: