21

Scala 中的列表没有隐式排序是否有原因?

val lists = List(List(2, 3, 1), List(2, 1, 3))
lists.sorted

error: could not find implicit value for parameter ord: Ordering[List[Int]]

编辑

是的,我的问题是为什么没有内置的 Ordering 已经隐含在范围内。对我来说,很明显第二个列表应该“小于”第一个列表,因为 0 处的项目是相等的,而第二个列表的较低项目位于 1。我想知道当列表有两种不同的大小。

4

7 回答 7

45

我认为这是一个疏忽。字典顺序在 Seqs 上确实有意义。我们应该将它添加到标准库中。

于 2010-12-20T21:24:28.093 回答
8

顺便说一句,即使在我修复此问题之前,您也可以通过其他方式执行此操作:

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))
于 2010-12-21T07:21:40.247 回答
4

您拥有的是列表列表,而不是整数列表。您缺少的是确定列表是否 <= 另一个列表的标准。

这就是错误消息所说的:我找不到将一个列表与另一个列表进行比较的方法,您应该明确提供一个。

如果您的问题是“为什么列表没有与其他列表的内置比较方法”,那么就是这样。

于 2010-12-20T19:44:25.783 回答
4

List[Int] 类上唯一真正合理的总顺序是字典顺序(即,比较列表的第一个元素,如果它们相等,则比较第二个,如果秒数相等,则第三个,等等)。这不是由标准库提供的,可能是因为实际需要它的情况并不多。创建一个从 List[X] 到 Ordering[List[X]] 的隐式转换来实现这一点很容易,然后您可以在任何需要的地方简单地导入该转换。

于 2010-12-20T20:08:13.280 回答
1

您可以使用 sortWith。这不会考虑不同大小的列表,因为 zip 会消除差异,但我认为它的作用类似于您所追求的:

lists.sortWith((a,b) => {
  a.zip(b).filterNot(x => x._1 == x._2) match {
    case Nil => true
    case t => t._1 < t._2
  }
})
于 2010-12-20T20:59:10.683 回答
0

在较新的 Scala 版本(使用 2.12.5 测试)中,有一个Ordering for Iterable[A]。只需将正确的类型归因于您的变量lists

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: List[Iterable[Int]]).sorted
res0: List[Iterable[Int]] = List(List(2, 1, 3), List(2, 3, 1))

Or convert the elements to instances of Iterable[] (which is a no-op for instances of List[]):

scala> lists.map(_.toIterable).sorted
res1: List[Iterable[Int]] = List(List(2, 1, 3), List(2, 3, 1))
于 2019-01-24T15:22:02.563 回答
0

Scala 2.13.3 - invoking sorted method worked for me:

import scala.math.Ordering.Implicits.seqOrdering
val lists = List(List(2, 3, 1), List(2, 1, 3))
print(lists.sorted)   // prints List(List(2, 1, 3), List(2, 3, 1)) 

sorted method is a member of StrictOptimizedSeqOps trait,:

override def sorted[B >: A](implicit ord: Ordering[B]): C
于 2021-01-30T17:37:53.600 回答