문제

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