Problem overview
The first version of the sort project is small. sortLines(lines: Array[String]): String receives an array of lines and returns them in ascending order, joined by \n. Duplicates are kept, and an empty array gives the empty string.
The order is by character code, like LC_ALL=C sort. That means every uppercase letter comes before every lowercase letter, so "Zebra" sorts before "apple", and an empty line sorts first of all. For ["banana", "apple", "cherry"] the answer is "apple\nbanana\ncherry".
Approach
Scala's standard library already knows how to order strings, so the job has two parts: sort, then join. lines.sorted sorts using the default ordering of the element type, and for String that ordering compares the characters' codes one by one. A shorter string that is a prefix of a longer one comes first, which is why the empty string leads.
What "by character code" means
Every Char has a numeric code. 'Z' is 90 and 'a' is 97, so any capital letter is smaller than any lowercase one. To compare two strings, Scala looks at the first position where they differ and orders them by the codes found there. If one string runs out first, it is the smaller one: "app" comes before "apple", and "" comes before everything.
This is the same rule the sort command applies when it runs in the plain C locale, and it is the reason the lesson's examples place "Zebra" ahead of "apple". Because the rule is the default ordering of String, you don't write any comparison yourself in this version.
Step by step
- Create a
val sortedLinesfromlines.sorted. The call returns a new array and leaveslinesas it was, so you don't copy anything yourself. - Join the sorted lines with
mkString("\n"). The argument goes between neighbouring elements only, so there is no trailing newline after the last line.
The solution in Scala
class Solution {
def sortLines(lines: Array[String]): String = {
val sortedLines = lines.sorted
return sortedLines.mkString("\n")
}
}
Details that matter here:
sortedneeds no comparison function becauseStringhas a built-in ordering. It works the same way for numbers, where it would order by value.- Sorting strings is not sorting numbers. The lines
["10", "9", "1"]come out as"1\n10\n9", because"10"is compared character by character and its first character'1'is smaller than'9'. - The method does not change its argument. After the call, an input array holding
c, a, bstill holdsc, a, b. mkStringon an empty array returns"", which is exactly what the statement asks for the empty case, so no special handling is needed.
Complexity
Sorting n lines takes O(n log n) comparisons, and each comparison costs up to the length of the strings involved. The sorted copy needs O(n) extra space, and so does the joined result.
Edge cases and common mistakes
- Capitals first.
["apple", "Zebra", "banana"]gives"Zebra\napple\nbanana". This is correct for this lesson, even though a dictionary would put"Zebra"last. - Empty lines.
["b", "", "a"]gives"\na\nb": the empty line sorts first, so the result starts with a newline. - Duplicates.
["b", "a", "b", "a"]gives"a\na\nb\nb"; nothing is removed. - Empty array. The result is
"".
