Start learning free

LESSON 623 OF 674 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: sort v1 — Ascending Alphabetical Sort

Practice

Build a tiny version of the Unix sort command. Given a list of lines, return them sorted in ascending order by character code (like LC_ALL=C sort: uppercase letters come before lowercase ones, "Zebra" before "apple", and an empty line comes first), joined by newline characters (\n). Duplicates are all kept. If the list is empty, return the empty string.

Function signature:

public string SortLines(string[] lines)

Example:

Input: lines = ["banana", "apple", "cherry"]

Output: "apple\nbanana\ncherry"

When printed:
apple
banana
cherry

Copy the input into a new List<string>, sort it in place with Sort(StringComparer.Ordinal), then stitch it together with string.Join.

The full solution:
class Solution {
    public string SortLines(string[] lines) {
        List<string> sortedLines = new List<string>(lines);
        sortedLines.Sort(StringComparer.Ordinal);
        return string.Join("\n", sortedLines);
    }
}

STEP 1 OF 3
    Ctrl + Enter to check