顺便说一句,即使在我修复此问题之前,您也可以通过其他方式执行此操作:
scala> List[Iterable[Int]](List(2, 3, 1), List(2, 1, 3)).sorted
res0: List[Iterable[Int]] = List(List(2, 1, 3), List(2, 3, 1))
scala> List(List(2, 3, 1), List(2, 1, 3)).sorted(Ordering[Iterable[Int]])
res1: List[List[Int]] = List(List(2, 1, 3), List(2, 3, 1))
但现在它就像你希望的那样工作。
编辑:由于必要隐含的粗略分歧问题,我将其移出默认范围。具有跨界的隐式转换,如下所示:
implicit def SeqDerived[CC[X] <: collection.Seq[X], T](implicit ord: Ordering[T]): Ordering[CC[T]]
...是一个潜在的问题的秘诀。它将在 2.9 中可用,但您必须按如下方式导入它。
scala> val lists = List(List(2, 3, 1), List(2, 1, 3))
lists: List[List[Int]] = List(List(2, 3, 1), List(2, 1, 3))
scala> lists.sorted
<console>:9: error: could not find implicit value for parameter ord: Ordering[List[Int]]
lists.sorted
^
scala> import Ordering.Implicits._
import Ordering.Implicits._
scala> lists.sorted
res1: List[List[Int]] = List(List(2, 1, 3), List(2, 3, 1))