Problem overview
The second version of the sort tool bakes in the -r flag. sortLines(_ lines: [String]) -> String returns the lines in descending order by character code, joined with "\n". It is the exact reverse of the previous version's order: lowercase letters now come before uppercase ones. Duplicates are kept, and an empty list gives "".
For ["banana", "apple", "cherry"] the result is "cherry\nbanana\napple".
Approach: tell sorted(by:) which element goes first
The previous version called sort(), which uses the default ascending order. To change the direction, give the sort an ordering rule. Swift's sorted(by:) takes a function that receives two elements and answers one question: should the first one come before the second? For ascending order the answer is a < b. For descending order it is a > b.
Operators are functions in Swift, so you can pass > itself as that rule. There is no need to write a closure like { $0 > $1 }, although that works the same way.
Step by step
- Create
sortedLineswithlet, fromlines.sorted(by: >).sorted(by:)returns a new array and leaveslinesalone, so novarcopy and no in-place call are needed. The>makes the larger string go first. - Return
sortedLines.joined(separator: "\n"). The separator goes between lines only, so there is no trailing newline.
The solution in Swift
func sortLines(_ lines: [String]) -> String {
let sortedLines = lines.sorted(by: >)
return sortedLines.joined(separator: "\n")
}
Details that matter:
sortedandsortcome in pairs.sorted(by:)returns a new array;sort(by:)changes avarin place. Both take the same ordering rule. This version uses the returning form, which is whysortedLinescan be alet.- Passing an operator.
by: >works because the compiler picks the>that takes twoStringvalues. The array's element type decides that, so the same code sorts an array of integers in descending order too. - The rule must be strict.
>says "strictly greater", which is what a sorting rule needs. Writing>=breaks that requirement and can lead to unpredictable results. - Character-code order. Strings compare character by character. Uppercase ASCII letters have smaller codes than lowercase ones, so in descending order
"apple"comes before"Zebra".
Complexity
Time: O(n log n) comparisons for n lines, each costing up to the length of the shorter line. Space: O(n), for the new sorted array and the joined result.
Edge cases and common mistakes
- Sorting first, then reversing. Calling
sorted()and then reversing the array gives the same order for strings, but it takes an extra step.sorted(by: >)says what you mean in one call. - Duplicates.
["b", "a", "b"]returns"b\nb\na". Every copy stays. - The empty line goes last. It is the smallest string, so in descending order
["b", "", "a"]returns"b\na\n". The output ends with a newline because the last line is empty. That is the correct result, not a trailing separator. - Lowercase before uppercase.
["apple", "Zebra"]returns"apple\nZebra". - Empty input. An empty array stays empty after sorting, and joining it gives
"". - Calling
sort(by: >)on aletvalue. That is a compile error, because sorting in place needs avar. Usesorted(by:)when the result goes into alet.
Testing
["banana", "apple", "cherry"]gives"cherry\nbanana\napple".["apple", "Zebra"]gives"apple\nZebra".["b", "", "a"]gives"b\na\n".["b", "a", "b"]gives"b\nb\na".[]gives"".
