How to sort lowercase and lowercase strings in Scala
Scala has a list of strings. Let's assume these strings contain only English letters (lowercase and uppercase). Here's a rough list:
val l1 = List("ab","aa", "bc","Aa", "Ab", "Ba", "BB")
When we sort it with the following code:
l1.sortWith(_ < _)
we'll get:
List(Aa, Ab, BB, Ba, aa, ab, bc)
so this sort uses the following letter-to-letter relationships:
A < B < C < ... < a < b < c ...
we can also use:
l1.sortWith(_.toLowerCase < _.toLowerCase)
receiving:
List(aa, Aa, ab, Ab, Ba, BB, bc)
So now the relationship between letters:
(a=A) < (b=B) < (c=C) ...
But how do you sort them in Scala using the following letter order?
a < A < b < B < c < C ...
So the result should be:
List(aa, ab, Aa, Ab, bc, Ba, BB)
+3
source to share
4 answers
scala> def compareChar(c1:Char, c2:Char) = {
if ( c1 == c2 ) None
else if (c1.toLower == c2.toLower) Some(c2.isUpper)
else Some(c1.toLower < c2.toLower)
}
compareChar: (c1: Char, c2: Char)Option[Boolean]
scala> def compareString(s1:String, s2:String) : Boolean = {
(s1 zip s2).collectFirst {
case (c1,c2) if (compareChar(c1,c2).isDefined) => compareChar(c1,c2).get
}.getOrElse(s1.length < s2.length)
}
compareString: (s1: String, s2: String)Boolean
scala> l1 sortWith compareString
res02: List[String] = List(aa, ab, Aa, Ab, bc, Ba, BB)
EDIT: Inline version:
def compareString(s1:String, s2:String) : Boolean = {
(s1 zip s2).collectFirst {
case (c1, c2) if c1 == c2 => compareString(s1.tail, s2.tail)
case (c1, c2) if c1.toLower == c2.toLower => c2.isUpper // same letter, different case, uppercase wins
case (c1, c2) => c1.toLower < c2.toLower
}.getOrElse(s1.length < s2.length) // same prefix, the longest string is bigger
}
scala> val l1 = List("ab","aa", "bc","Aa", "Ab", "Ba", "BB")
l1: List[String] = List(ab, aa, bc, Aa, Ab, Ba, BB)
scala> l1 sortWith compareString
res0: List[String] = List(aa, ab, Aa, Ab, bc, Ba, BB)
scala> List("ABC","AB") sortWith compareString
res1: List[String] = List(AB, ABC)
+5
source to share
scala> import math.Ordering.Implicits._
import math.Ordering.Implicits._
scala> val words = List("ab","aa", "bc","Aa", "Ab", "Ba", "BB")
words: List[String] = List(ab, aa, bc, Aa, Ab, Ba, BB)
scala> words sortBy (_ map (c => if (c.isUpper) 2 * c + 1 else 2 * (c - ('a' - 'A'))))
res0: List[String] = List(aa, ab, Aa, Ab, bc, Ba, BB)
+2
source to share
try it
val l1 = List("ab","aa", "bc","Aa", "Ab", "Ba", "BB")
def comp[T <: String](a: T, b: T) = {
def _comp(i: Int): Boolean = try {
val (l, r) = (a(i), b(i))
if (l == r) _comp(i+1) else l.toLower < r.toLower
} catch {
case e: IndexOutOfBoundsException => true
}
_comp(0)
}
println(l1.sortWith(comp)) // List(aa, ab, Aa, Ab, bc, Ba, BB)
0
source to share
Optimized solution
tailrec
:
def comp(x: String, y: String): Boolean = {
@tailrec
def go(xs: List[Char], ys: List[Char]): Boolean = {
(xs, ys) match {
case (hx :: tx, hy :: ty) =>
if (hx == hy) go(tx, ty)
else if (hx.toLower == hy.toLower) hx.isLower
else if (hx.isLower) hx < hy
else hx < hy.toUpper
case (Nil, _) => true
case (_, Nil) => false
}
}
go(x.toList, y.toList)
}
0
source to share