Problem overview
The sort project starts with its simplest form. sortLines(lines: Array<String>): String takes the lines, sorts them in ascending order by character code, and returns them joined with \n. Duplicates stay, and an empty array gives the empty string.
"By character code" means every character is compared by its numeric value. In that order every uppercase letter comes before every lowercase letter, so "Zebra" sorts before "apple", and an empty line comes first of all. For ["banana", "apple", "cherry"] the result is "apple\nbanana\ncherry".
Approach
Kotlin's standard library already knows how to order strings, so the work is three small moves: copy the input into a list you can reorder, sort that list, and glue the lines together. You don't write a sorting algorithm.
The default order of String compares character by character, using each character's numeric code, and a shorter string that is a prefix of a longer one comes first. That is the order the lesson asks for, so plain sort() needs no comparator.
Step by step
- Copy the array into a mutable list.
lines.toMutableList()builds a newMutableList<String>. AMutableListcan be sorted in place, and the original array stays untouched. - Sort it in place.
sortedLines.sort()reorders the list itself and returns nothing. - Join with newlines.
joinToString("\n")places the separator between lines only, so there is no newline after the last one.
The solution in Kotlin
class Solution {
fun sortLines(lines: Array<String>): String {
val sortedLines = lines.toMutableList()
sortedLines.sort()
return sortedLines.joinToString("\n")
}
}
Kotlin details in this code
sort()againstsorted(). On aMutableList,sort()changes the list and returnsUnit.sorted()leaves the list alone and returns a new sortedList. This is why the solution callssort()on its own line and then joinssortedLines. An equivalent one-liner islines.sorted().joinToString("\n").- Why copy first. An
Arrayhas its ownsort()that reorders it in place. Using the copy means the caller's array is not changed behind its back. After callingsortLines(arrayOf("b", "a")), the original array still reads[b, a]. - The sort is stable, so equal lines keep their original relative order. For plain strings you can't see the difference, but it matters in later versions of the project, where lines that compare equal can still be different.
joinToStringwith an empty list returns"", so the empty case needs no special code.
Complexity
Sorting n lines takes O(n log n) comparisons, and each comparison costs up to the length of the shorter string. Copying and joining are O(n) in the number of lines. The extra space is the copied list plus the result string.
Edge cases and common mistakes
- Capitals first.
["apple", "Zebra", "", "banana", "apple"]gives the lines"","Zebra","apple","apple","banana". Don't try to make the sort case-insensitive; this lesson wants the raw character order. - Empty line first. The empty string is shorter than any other string with the same start, so it sorts before all of them. The joined result then begins with a newline.
- Duplicates are kept. Sorting doesn't remove anything.
- Empty array. The result is
"". - Using the return value of
sort(). Writingreturn lines.toMutableList().sort().joinToString("\n")doesn't compile, becausesort()returnsUnit, which has nojoinToString.
