Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Sorting data and why it matters

After this lesson: I can sort data and say why it matters.

45 min lessonNo commands

The manager wants the top sellers

Your manager asks for this week's five best-selling products. The sales arrive in the order they happened.

Put them in order first, and the answer is the first five rows.

Sorted data makes reports readable, puts repeats next to each other, and lets binary search work.

By the end of this lesson

Note

I can sort data and say why it matters.

Key words

WordWhat it meansExample
Sortto put items in order
Ascendingsmallest first, or A to Z
Descendinglargest first, or Z to Areverse=True
Swapto make two items change places
Passone trip through the whole list
Bubble sorta sort that swaps neighbours in the wrong order

The cells students fill in are left blank in this preview.

Python sorts a list for you

sorted(sales) gives you a new list in ascending order. The original list stays as it was.

sales.sort() puts the list itself in order. Nothing new is made.

Add reverse=True to either for descending order. Text sorts A to Z in the same way.

Try it: sort the week's sales

Code: python
# Sales this week, in pounds, in the order they came in.
sales = [120, 45, 310, 80, 205]

print(sorted(sales))                 # a new list, smallest first
print(sorted(sales, reverse=True))   # a new list, largest first
print(sales)                         # the original is unchanged

sales.sort()                         # sorts the list itself
print(sales)

names = ["Wei", "Amara", "Owen", "Bea"]
print(sorted(names))                 # text sorts A to Z

# Print only the top three sales, then press Run.

A bubble sort swaps neighbours

Inside, a sort is just compares and swaps. A bubble sort is the easiest one to follow.

It compares the first two items. If they are the wrong way round, it swaps them. Then it moves one place along and does the same.

One trip through the list is a pass. It keeps making passes until a pass needs no swaps.

Worked example: sorting 5, 2, 4, 1

PassList after the passSwaps in the pass
Start5, 2, 4, 1none yet
12, 4, 1, 53: the 5 moves to the end
22, 1, 4, 51: the 4 and the 1
31, 2, 4, 51: the 2 and the 1
41, 2, 4, 50: so the list is sorted

The cells students fill in are left blank in this preview.

Try it: a bubble sort

Code: python
numbers = [5, 2, 4, 1]

passes = 0
swapped = True
while swapped:
    swapped = False
    passes = passes + 1
    for index in range(len(numbers) - 1):
        # Compare each pair of neighbours.
        if numbers[index] > numbers[index + 1]:
            # Wrong way round, so swap them.
            first = numbers[index]
            numbers[index] = numbers[index + 1]
            numbers[index + 1] = first
            swapped = True
    print("After pass", passes, numbers)

# Change the list to [9, 7, 3, 8, 1], then press Run.

A common mistake

Warning

Many people think numbers always sort as numbers. Numbers read from a file or a form are often text. Text sorts one character at a time, so "100" comes before "25". Turn each one into a number with int() before you sort.

See it: numbers stored as text

Code: python
# Order quantities read from a spreadsheet arrive as text.
quantities = ["9", "100", "25", "3"]
print(sorted(quantities))     # sorted as text, character by character

numbers = []
for one in quantities:
    numbers.append(int(one))  # turn each one into a whole number
print(sorted(numbers))        # sorted as numbers

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.