0

They are pretty much the same thing. Yes, only the first of the following codes can be compiled:

sorting String:

val ss = Seq(
  "abc",
  "def"
)

ss.sorted

sorted Seq:

val ss = Seq(
  "abc",
  "def"
)
.map(_.toSeq)

ss.sorted

What's the point of such design?

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
tribbloid
  • 4,026
  • 14
  • 64
  • 103

2 Answers2

2

You need to add

import scala.math.Ordering.Implicits._

(or scala.math.Ordering.Implicits.seqOrdering).

Why you need this extra import is in the documentation:

Not in the standard scope due to the potential for divergence: For instance implicitly[Ordering[Any]] diverges in its presence.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
1

I think that String and Seq[Char] are isomorphic but not same. They are 'same' in the same way as Int and (Byte, Byte, Byte, Byte). String has additional semantics.

If you add Ordering[Seq[Char]] then it would be consistent to have Ordering[A : Ordered]. And it looks quite opinionated to be in stdlib.

Anyways it's not a 'design' problem since nothing stops you from writhing an instance of Ordering[Seq[Char]] yourself.

simpadjo
  • 3,947
  • 1
  • 13
  • 38
  • Except it _is_ in stdlib, just behind an import for technical reasons. – Alexey Romanov Aug 15 '19 at 02:19
  • See also https://stackoverflow.com/a/4494095/9204: "I think it's an oversight. Lexicographic ordering does make sense on Seqs. We should add it to the standard library." (in 2010) – Alexey Romanov Aug 15 '19 at 02:30