Problem overview
The second version of the sort project adds the -r flag. sortLines(lines: List<String>): String sorts the lines in descending order by character code, which is exactly the reverse of the previous version, and joins them with \n. Duplicates stay and an empty list gives "".
Because the order is by character code, lowercase letters now come before uppercase ones and an empty line comes last. For ["banana", "apple", "cherry"] the result is "cherry\nbanana\napple".
One small change from the previous version: the input is now a List<String> instead of an Array<String>. A List in Kotlin is read-only, so the function can't sort it directly, and copying it is the natural first step.
Approach
You could sort ascending and then reverse the list. Kotlin has a method that does both in one go, sortDescending(), so the whole function stays at three lines: copy, sort, join. The lesson doesn't need a custom comparator.
Step by step
- Copy.
lines.toMutableList()makes a list you are allowed to change. The read-onlyListthat was passed in has no sorting-in-place methods, and the caller's list stays as it was. - Sort, biggest first.
sortedLines.sortDescending()reorders the copy in place so that the largest string comes first. - Join.
joinToString("\n")separates the lines with a newline, with none after the last line.
The solution in Kotlin
class Solution {
fun sortLines(lines: List<String>): String {
val sortedLines = lines.toMutableList()
sortedLines.sortDescending()
return sortedLines.joinToString("\n")
}
}
Kotlin details in this code
- Read-only does not mean immutable. A
List<String>parameter is a read-only view: you can't callsort()oradd()on it. Copying withtoMutableList()is the usual way to get something you may reorder. sortDescending()againstsortedDescending(). The first sorts a mutable list in place and returnsUnit. The second returns a new sorted list and leaves the original alone. The equivalent one-liner islines.sortedDescending().joinToString("\n").- The order is still by character code.
sortDescending()uses the same comparison assort(), just reversed, which is what the lesson asks for.
Complexity
Sorting takes O(n log n) comparisons for n lines, and the copy and the join add O(n). The extra space is the copied list and the result string.
Edge cases and common mistakes
- Lowercase first. For
["apple", "Zebra", "", "banana", "apple"]the lines come out as"banana","apple","apple","Zebra","". The empty line is last, so the output ends with a newline character. - Reversing the joined string. Reversing the final string would reverse every line's characters as well. Reverse the order of the lines, not the text inside them.
- Duplicates. Both copies of
"apple"stay in the output. - Empty list. The result is
"". - Sorting the parameter directly.
lines.sortDescending()doesn't compile on a read-onlyList. That is the reason for the copy.
Testing
Inputs worth trying, with the results the solution returns (shown with \n written as a vertical bar):
| Input | Result |
|---|---|
["banana", "apple", "cherry"] | cherry|banana|apple |
["apple", "Zebra", "", "banana", "apple"] | banana|apple|apple|Zebra| |
[] | the empty string |
The last line of the second result is an empty line, so the string ends with a newline. Check that your output matches in all three cases before moving on. If the first one comes out in ascending order, you probably called sort() instead of sortDescending().
