Start learning free

LESSON 389 OF 410 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: sort v1 — Ascending Alphabetical Sort in Python

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:

def sortLines(self, lines: List[str]) -> str:

Example:

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

Output: "apple\nbanana\ncherry"

When printed:
apple
banana
cherry

Use Python's built-in sorted for ascending order, then join with "\n".

The full solution:
class Solution:
    def sortLines(self, lines):
        sortedLines = sorted(lines)
        return "\n".join(sortedLines)

STEP 1 OF 2
    Ctrl + Enter to check

    Sort V1 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free