rtruszk
rtruszk

Reputation: 3922

How to sort uppercase and lowercase Strings in Scala

There is a list of Strings in Scala. Lets assume that these Strings contain only English letters (lowercase and uppercase). Here is exemplary list:

val l1 = List("ab","aa", "bc","Aa", "Ab", "Ba", "BB")

When we sort it with following code:

l1.sortWith(_ < _)  

we will receive:

List(Aa, Ab, BB, Ba, aa, ab, bc)

so this sorting uses following relations between letters:

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 relations between letters are:

(a=A) < (b=B) < (c=C) ...

But how to sort them in Scala using following letters order? :

a < A < b < B < c < C ...

So the result should be;

List(aa, ab, Aa, Ab, bc, Ba, BB)

Upvotes: 3

Views: 2829

Answers (4)

user5102379
user5102379

Reputation: 1512

tailrec optimized solution:

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

Upvotes: 0

Marth
Marth

Reputation: 24802

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

Upvotes: 5

som-snytt
som-snytt

Reputation: 39577

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)

Upvotes: 2

blueiur
blueiur

Reputation: 1507

Try this

  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)

Upvotes: 0

Related Questions