Build the next version of the Unix sort command, this time with the -r flag baked in. Given a list of lines, return them sorted in descending order by character code (the exact reverse of v1's order, like LC_ALL=C sort -r: lowercase letters come before uppercase ones), joined by newline characters (\n). Duplicates are all kept. If the list is empty, return the empty string.
Function signature:
public string SortLines(List<string> lines)
Example:
Input: lines = ["banana", "apple", "cherry"] Output: "cherry\nbanana\napple" When printed: cherry banana apple
Sort with a comparator that compares in reverse, e.g.
string.Compare(b, a, StringComparison.Ordinal), then combine with string.Join("\n", ...).
The full solution:
class Solution {
public string SortLines(List<string> lines) {
List<string> sortedLines = new List<string>(lines);
sortedLines.Sort((a, b) => string.Compare(b, a, StringComparison.Ordinal));
return string.Join("\n", sortedLines);
}
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
