Pybites Logo Rust Platform

sort: Sort Lines

Medium +3 pts
Unix tools 8/10

🎯 sort orders lines alphabetically; sort -n orders them as numbers. The difference matters: as text, "10" sorts before "9" (it compares character by character), with -n 9 does come before 10.

In Python you can accomplish this using the key argument of the sorted built-in:

def sort_lines(text: str, numeric: bool = False) -> list[str]:
    lines = text.splitlines()
    return sorted(lines, key=int) if numeric else sorted(lines)

Sorting needs an owned, mutable collection

To sort a Vec in-place you need to use mut:

let mut v = vec!["banana", "apple", "cherry"];
v.sort();   // ["apple", "banana", "cherry"] — lexical, like Python's sorted()

sort_by_key — sort by a derived value

Lexical order puts "10" before "9". For numeric order, you can use sort_by_key which takes a closure mapping each element to something comparable, similar how we use key in Python:

let mut v = vec!["ccc", "a", "bb"];
v.sort_by_key(|s| s.len());   // ["a", "bb", "ccc"] — neutral: order by length

Parsing a &str to a number returns a Result, so you'll need to turn that into a plain comparable key that treats non-numbers as 0 (the parse reference below points the way).

Login to see the full task and start coding.

This is a premium exercise

Log in to unlock the full exercise and start coding.

Login to access this exercise