Why don't Scala Lists have an Ordering?

I think it's an oversight. Lexicographic ordering does make sense on Seqs. We should add it to the standard library.


Incidentally even before I fixed this you could do this other ways:

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))

But now it works like you'd hope.

Edit: due to sketchy divergence issues with the requisite implicit I moved it out of the default scope. Having an implicit conversion which acts across a bound like this:

implicit def SeqDerived[CC[X] <: collection.Seq[X], T](implicit ord: Ordering[T]): Ordering[CC[T]]

...is a potential recipe for issues. It'll be available in 2.9, but you have to import it as follows.

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))

What you have is a list of lists, not a list of integers. What you are missing is a criteria for determining whether a list is <= another list, or not.

That's what the error message says: I can't find a way to compare a list to another list, you should provide one explicitly.

If your question was "why don't list have a built-in comparison method against other lists", well, that's just the way it is.