Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Sort a list of numbers ascending and descending

Return a sorted copy of a list of numbers in ascending or descending order.

Python practice10 minLists & TuplesBeginnerLast updated March 17, 2026

Problem statement

Write a function sort_numbers(nums, reverse=False) that takes a list of numbers (ints and/or floats) and returns a new list sorted in ascending order when reverse is False, or in descending order when reverse is True. Do not mutate the original list.

Task

Implement a function to return a new list sorted in ascending order by default or descending when requested.

Examples

Ascending sort (default)

Input

sort_numbers([3, 1, 2])

Output

[1, 2, 3]

Default returns numbers sorted ascending.

Input format

A list of numbers and an optional boolean reverse flag.

Output format

A new list of numbers sorted in the requested order.

Constraints

Should handle empty lists and a mix of ints and floats. Aim for O(n log n) time using Python's sorting facilities.

Samples

Sample 1

Input

sort_numbers([3, 1, 2], True)

Output

[3, 2, 1]

reverse=True returns numbers sorted descending.