Problem overview
This version adds the effect of sort -r. The method sortLines returns the lines in descending order by character code, joined by "\n". Duplicates are kept and an empty list gives the empty string.
For ["banana", "apple", "cherry"] the result is "cherry\nbanana\napple". It is the exact reverse of the previous version's order, so lowercase letters now come before uppercase ones and the empty line comes last.
Approach
You could sort ascending and then reverse the list, but sorted has a named argument for this: reverse=True. It still compares the strings the same way, only the other way round.
Reversing an ascending result by hand and using reverse=True can differ when elements compare equal, because reverse=True keeps equal elements in their original relative order. For plain strings that makes no visible difference, since equal strings are identical, but it is the reason to prefer the argument over slicing the result.
Python compares strings by character code, so the reversed order is the descending one: "Zebra" < "apple" is true (code 90 against 97), and in descending order "apple" therefore comes first. For ["apple", "Zebra", "banana"], both sorted(l, reverse=True) and sorted(l)[::-1] give ['banana', 'apple', 'Zebra'].
The stability remark is easy to see once sorting uses a key function. Sorting ["bb", "a", "cc", "d"] by length with reverse=True gives ['bb', 'cc', 'a', 'd']: "bb" stays ahead of "cc" and "a" ahead of "d", as in the input. Reversing the ascending result by slicing gives ['cc', 'bb', 'd', 'a'] instead, with each tie flipped. The next versions of this tool rely on such keys, so the difference is worth knowing now.
Step by step
- Create
sortedLinesfromsorted(lines, reverse=True). Biggest first. - Return the lines joined with a newline between each two.
The solution in Python
class Solution:
def sortLines(self, lines):
sortedLines = sorted(lines, reverse=True)
return "\n".join(sortedLines)
reverse is a keyword argument, so it has to be written by name: sorted(lines, True) is not allowed. Passing it positionally fails with TypeError: sorted expected 1 argument, got 2. As before, sorted returns a new list and leaves lines alone.
The function never modifies the list it receives. Sorting ["b", "a", "c"] with reverse=True gives ['c', 'b', 'a'], and the original list is still ['b', 'a', 'c'] afterwards. Callers can keep using their data, which is why sorted is the right tool here and the in-place list.sort is not.
Complexity
The time is O(n log n) for n lines and the extra space is O(n) for the new list, the same as the ascending version.
Edge cases and common mistakes
| lines | result |
|---|---|
["banana", "apple", "cherry"] | "cherry\nbanana\napple" |
["apple", "Zebra", "", "banana", "apple"] | "banana\napple\napple\nZebra\n" |
["b"] | "b" |
[] | "" |
- In the second row the empty line sorts last, so the result ends with a newline character. That is the join doing its job: the empty string is the final item, with a separator before it.
- Descending order does not mean "reverse the input".
["b", "a", "c"]becomesc, b, awhatever order it arrived in. - The comparison is still textual.
sorted(["10", "9", "1"], reverse=True)gives['9', '10', '1'], because"9"is the biggest first character. Sorting by number needs a different approach, which a later version covers. - Uppercase comes after lowercase here, so
"Zebra"follows"apple". - Don't call
lines.sort(reverse=True)and join its return value:list.sortreturnsNone.
