Question

How can I sort my Tag elements by id ?

I don't get how to define the orderById function...

case class Tag(id: Int, name: String, ttype: String)

val orderByID = Ordering[??].on[Tag](? => ? -> ?)

val mySet: SortedSet[Tag] = SortedSet()(orderByID) ;
Was it helpful?

Solution

I think you need:

val orderByID = Ordering.by[Tag, Int](_.id)
val mySet: SortedSet[Tag] = SortedSet()(orderByID)

OTHER TIPS

Ordering.on turns an Ordering[U] into an Ordering[T] given a function to produce a U from a T. In this case, we need to start with an Ordering[Int], then provide a Tag => Int function:

val orderByID = Ordering[Int].on[Tag](_.id)

But as @Lee has said, Ordering.by is a more straightforward approach here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top