September 2018
Intermediate to advanced
398 pages
9h 43m
English
Ordering is an SDK type class that represents a strategy to sort the instances of a type. The most common use case is to sort the elements of a collection, as follows:
Vector(1,3,2).sorted // res0: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
If you look at the declaration of sorted, you will see that it accepts an implicit, Ordering[B]:
def sorted[B >: A](implicit ord: Ordering[B]): Repr
When we called sorted on our Vector[Int], the compiler found an implicit value of the type Ordering[Int] to pass to the function. This implicit was found in the companion object of Ordering, which also defines instances for String, Long, Option, Tuples, and so on.
We can define an instance of the type class for LocalDate
Read now
Unlock full access