gsmafra gsmafra - 6 days ago 5
Scala Question

Scala method that returns the first string found in the alphabet/dictionary

For example this code

val stringTuple = ("BLACK", "GRAY", "WHITE")
firstInAlphabet(stringTuple)


Should return
"BLACK"
. How would you define
firstInAlphabet
?

Personally I prefer simple and fast implementations over complicated ones that would cover a lot of cases.

Answer
stringTuple.toString.tail.init.split(",").sorted.head

Turn the tuple into a single string, trim the parentheses out, split on the comma, sort, take the 1st element.