Problem overview
The second version of the sort project adds the effect of the -r flag. sortLines still takes an array of strings and returns one string joined by \n, but now the order is descending by character code, the exact reverse of the previous version: lowercase letters come before uppercase ones. Duplicates are kept, and an empty array gives "".
For ["banana", "apple", "cherry"] the result is "cherry\nbanana\napple".
Approach: ascending order, turned around
The previous version already knows how to sort by character code with the default sort(). Descending order is that same order read backwards, so one extra call is enough: reverse(), which flips an array in place and returns it. Sort first, reverse second.
The same warning as before applies to both methods. sort() and reverse() both modify the array they are called on, so the code must work on a copy, made with the spread [...lines]. Chaining makes it short: the copy is sorted, and the sorted copy is reversed, so the caller's array is never touched.
An alternative is a comparator that compares the other way round. It works too, but the sort-then-reverse version states the idea plainly, and for strings that are equal the two orders cannot be told apart, so ties raise no question.
One more reason the order matters: reversing the ascending result is only correct because the ascending order is a total order on the lines. Two equal lines are identical strings, so it makes no difference which one ends up first.
Step by step
- Copy, sort and reverse. Create
const sortedLines: string[]from[...lines].sort().reverse(). The spread makes the copy,sort()puts it in ascending order andreverse()flips it, so the largest string comes first. - Join with newlines. Return
sortedLines.join("\n"). As before,joinplaces the separator only between lines, so there is no trailing newline.
The solution in TypeScript
class Solution {
sortLines(lines: string[]): string {
const sortedLines: string[] = [...lines].sort().reverse();
return sortedLines.join("\n");
}
}
Language details
- Method chaining works because
sort()returns the array it sorted, so.reverse()can be called on the result. Both calls act on the copy created by the spread. - Without a comparator,
sort()compares strings by UTF-16 code unit, which for ASCII text is the character code. That is why"b"ends up before"B"in the reversed result. - Reversing is not the same as sorting with
localeCompareor sorting case-insensitively. This lesson wants the raw code order.
Complexity
The copy and the reverse() are O(n) and the sort is O(n log n), so the total time is O(n log n) comparisons. The copy and the final string use O(n) extra space.
Edge cases and common mistakes
- Forgetting
reverse(). The result would be ascending, identical to the previous version. - Mixed case.
["b", "B", "a", ""]gives"b\na\nB\n". Lowercase letters come first, then uppercase ones, and the empty line comes last, because in descending order the smallest string is last. The text ends with a newline because its last line is empty. - Duplicates. They stay:
["a", "c", "a"]gives"c\na\na". - Mutating the input. Skip the spread and the caller's array is sorted and reversed in place.
- Empty input.
[]gives"".
Testing
| Input | Result |
|---|---|
["banana", "apple", "cherry"] | "cherry\nbanana\napple" |
["b", "B", "a", ""] | "b\na\nB\n" |
["a", "c", "a"] | "c\na\na" |
[] | "" |
