它们几乎是同一件事。是的,只能编译下面的第一个代码:
排序字符串:
val ss = Seq(
"abc",
"def"
)
ss.sorted
排序后的序列:
val ss = Seq(
"abc",
"def"
)
.map(_.toSeq)
ss.sorted
这样的设计有什么意义?
答案 0 :(得分:2)
您需要添加
import scala.math.Ordering.Implicits._
(或scala.math.Ordering.Implicits.seqOrdering
)。
文档中为什么需要这种额外的导入:
答案 1 :(得分:1)
我认为String
和Seq[Char]
是同构的,但不相同。它们与Int
和(Byte, Byte, Byte, Byte)
相同。 String
具有其他语义。
如果添加Ordering[Seq[Char]]
,则拥有Ordering[A : Ordered]
是一致的。而且看来它应该放在stdlib中。
无论如何,这不是一个“设计”问题,因为没有什么可以阻止您自己编写Ordering[Seq[Char]]
实例。