Basic

What sorting is for

Sorting puts a row of items in a chosen order. Order is what makes a list searchable, comparable, and readable, so sorting is one of the most common steps in a program.

What you will see

2

About 2 min of reading

Try It

Sort small rows and read the result.

1

Sort three numbers.

print(sorted([4, 1, 3]))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[1, 3, 4]
2

Sort high to low.

print(sorted([4, 1, 3], reverse=True))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[4, 3, 1]
3

Sort three words.

print(sorted(["owl", "ant", "bee"]))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
['ant', 'bee', 'owl']
4

Read the smallest after sorting.

nums = sorted([9, 2, 7]) print(nums[0])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2

Data Flow

Sorting arranges a row of items in a chosen order by comparing them, and that order is what later steps rely on.
1 a row of items
↓
2 compare two items
↓
3 place them in order
↓
4 the row is now searchable
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

print(sorted([4, 1, 3])) [1, 3, 4]
Why this works
Order is a contract

Once a list is sorted, every later step assumes it. A search, a merge, and a display all read that order, so the choice of order has to be shared.

Comparison is the engine

Every sorting method works by asking whether one item comes before another. Change the comparison and the same method sorts by something else.

Examples

A row out of order

nums = [4, 1, 3]
print(sorted(nums))

The row becomes 1, 3, 4. sorted returns a new row in order and leaves the original alone.

Order is a choice

print(sorted([4, 1, 3], reverse=True))

The same numbers can go low to high or high to low. The order you pick is the contract for everything that reads the list.

Words sort too

print(sorted(["owl", "ant", "bee"]))

Words sort by their letters. The rule is the same, only the comparison changes from numbers to letters.

Why order matters

nums = sorted([9, 2, 7])
print(nums[0])

Once a row is sorted, the smallest is the first item. Order turns a question into an index.

Sorting does not guess

print(sorted([2, 2, 1]))

Equal items stay together. Sorting decides position by comparison, never by luck.

A list of pairs

print(sorted([[2, "b"], [1, "a"]]))

Pairs sort by their first item, then the second. That is the rule behind sorting by a key.

Already sorted

print(sorted([1, 2, 3]))

A row in order sorts to itself. Sorting an ordered row is the cheapest case for every method.

Sorting is everywhere

print(sorted([5, 3, 4]))

Names, scores, dates, and files are all sorted. The same idea covers all of them.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Order that is not numbers
Order that is not numbers →