Basic

Order that is not numbers

Sorting needs a rule for what comes first, and that rule is not always smaller-is-first. Dates, names, and custom keys each set their own order.

What you will see

[[2, "a"], [1, "b"]]

About 2 min of reading

Try It

Sort by different keys.

1

Sort words by length.

words = ["bee", "owlww", "ox"] print(sorted(words, key=len))

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

What it prints
['ox', 'bee', 'owlww']
2

Sort ignoring case.

print(sorted(["Owl", "ant"], key=str.lower))

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

What it prints
['ant', 'Owl']
3

Sort number-strings by value.

print(sorted(["10", "9", "100"], key=int))

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

What it prints
['9', '10', '100']
4

Sort rows by the second field.

rows = [[1, "b"], [2, "a"]] print(sorted(rows, key=lambda r: r[1]))

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

What it prints
[[2, "a"], [1, "b"]]

Data Flow

A sort orders by a key, which is a value measured from each item, so any field or rule can set the order.
1 choose a key
↓
2 measure each item
↓
3 order by the measures
↓
4 items keep their data
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.

words = ["bee", "owlww", "ox"] sets nothing on screen
print(sorted(words, key=len)) ['ox', 'bee', 'owlww']
Why this works
The key is measured once

A sort with a key measures every item once, then orders by those measures. That is why a key can be expensive and still be fast.

Text order is letter order

Sorting text compares characters, so digits sort wrong and case sorts wrong. The key is how both are corrected.

Examples

Sort by length

words = ["bee", "owlww", "ox"]
print(sorted(words, key=len))

The key says measure each word by length. The shortest comes first, and the words themselves are untouched.

Sort case-insensitive

print(sorted(["Owl", "ant"], key=str.lower))

Uppercase letters sort before lowercase by default. Lowercasing each item first fixes the order.

Sort by the second field

rows = [[1, "b"], [2, "a"]]
print(sorted(rows, key=lambda r: r[1]))

The key picks which field decides order. Here the rows sort by their text, not their number.

Reverse a key

print(sorted([1, 3, 2], key=lambda x: -x))

Negating the key flips the order. That is how to sort descending by a computed value.

Dates sort as text

print(sorted(["2026-01-02", "2025-12-31"]))

Dates written year first sort correctly as text. The order of the fields is what makes that work.

A bad key

print(sorted(["10", "9", "100"]))

As text, 100 sorts before 9. The items are strings, so the comparison is letter by letter.

Fix the bad key

print(sorted(["10", "9", "100"], key=int))

Measuring each by its number value gives 9, 10, 100. The key turns text into a comparable number.

Keys are reusable

rows = [(3, "c"), (1, "a")]
print(sorted(rows, key=lambda r: r[0]))

Any function can be a key. The sort calls it once per item to get the value to order by.

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.

← What sorting is for
Previous Lesson
What sorting is for
Next Lesson
Bubble sort
Bubble sort →