質問

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) ;
役に立ちましたか?

解決

I think you need:

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top