proxyfss
proxyfss

Reputation: 113

How can I modify this Ordering?

I have this piece of code which call the default comparator for Longs:

val ordering = implicitly[Ordering[Long]]

How can I create my own ordering and call it with implicitly?

I tried this:

trait MyOrdering extends Ordering[Long] {
  override def compare(x: Long, y: Long): Int = x compare y
}

val ordering = implicitly[MyOrdering]

But it doesn't work

Upvotes: 1

Views: 51

Answers (1)

jwvh
jwvh

Reputation: 51271

You need to put an instance of your new Ordering implementation into the implicit namespace.

implicit val mo :MyOrdering = new MyOrdering {}

val ordering = implicitly[MyOrdering]

You could also change your trait to an implicit object but you'll also need to modify the implicitly parameter.

implicit object MyOrdering extends Ordering[Long] {
  override def compare(x: Long, y: Long): Int = x compare y
}

val ordering = implicitly[MyOrdering.type]

Upvotes: 2

Related Questions