Discover Patterns with Sorting
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Ready To Sort
From exploring to arranging
In Exploring Data Using Colors and Shapes, you looked at data using clues like color, shape, and count.
Now we will do a powerful next step: sorting.
Sorting means putting things in a helpful order, like:
- Smallest to biggest
- Biggest to smallest
- A to Z
- Same colors together
- Same shapes together
Why does this matter for data science, AI, and machine learning?
Because messy data can hide patterns. Sorted data is like toys lined up on a shelf: it becomes easier to see what is tiny, huge, repeated, missing, or unusual.

What Sorting Does
Sorting changes position, not meaning
Imagine these bubble sizes:
8, 2, 5, 2, 9
If we sort them from smallest to biggest:
2, 2, 5, 8, 9
The data did not become different data.
We still have:
- Two
2s - One
5 - One
8 - One
9
Sorting only changes the order so our eyes and computers can notice patterns more easily.
A simple way to say it:
Sorting is arranging data so patterns become easier to see.

Python Sorts Numbers
Let Python line up the numbers
Python has a helper called sorted().
It takes a collection of values and gives back a new sorted collection.
This is useful because we can keep the original data and also look at the arranged version.
```python
bubble_sizes = [8, 2, 5, 2, 9]
small_to_big = sorted(bubble_sizes)
big_to_small = sorted(bubble_sizes, reverse=True)
print("Original bubbles:", bubble_sizes)
print("Smallest to biggest:", small_to_big)
print("Biggest to smallest:", big_to_small)
print("Smallest bubble:", small_to_big[0])
print("Biggest bubble:", small_to_big[-1])
```
Sorting Meaning
A list of numbers is sorted from smallest to biggest.
What changed?
Patterns Become Visible
What can sorted data show?
When numbers are messy, patterns can hide.
Unsorted:
4, 10, 5, 4, 11, 100, 6
Sorted:
4, 4, 5, 6, 10, 11, 100
Now we can see more clearly:
-
Repeats:
4appears twice -
Close friends:
4, 5, 6are near each other -
Another close group:
10, 11 -
A very different value:
100is far away
That very different value is often called an outlier. It is not automatically bad. It just says, “Please look at me!”
In data science, sorting helps us ask better questions before making decisions.

Sort By Clues
Sorting is not only for numbers
In the last lesson, you used features like color and shape.
We can sort data by one feature at a time.
For example, each toy below has:
- A
color - A
shape
Python can sort by color first, or by shape first.
```python
toys = [
{"color": "red", "shape": "circle"},
{"color": "blue", "shape": "square"},
{"color": "red", "shape": "triangle"},
{"color": "green", "shape": "circle"},
{"color": "blue", "shape": "circle"}
]
by_color = sorted(toys, key=lambda toy: toy["color"])
by_shape = sorted(toys, key=lambda toy: toy["shape"])
print("Sorted by color:")
for toy in by_color:
print(toy)
print("\nSorted by shape:")
for toy in by_shape:
print(toy)
```
Same Data
True or false?
If we sort red, blue, and green toys by color, we still have the same toys. They are just arranged differently.
Missing Word
Fill in the blank:
Putting data into a helpful order is called being ______.
Find Big Gaps
Sorting helps us compare neighbors
Once numbers are sorted, each number has a left neighbor and a right neighbor.
That makes it easier to ask:
- Which values are close together?
- Where is the biggest jump?
- Is one value far from the others?
This is a small but important data science habit: sort first, then compare nearby values.
```python
scores = [52, 55, 53, 90, 54, 56]
sorted_scores = sorted(scores)
print("Sorted scores:", sorted_scores)
biggest_gap = 0
gap_start = None
gap_end = None
for i in range(len(sorted_scores) - 1):
left = sorted_scores[i]
right = sorted_scores[i + 1]
gap = right - left
print("Gap from", left, "to", right, "=", gap)
if gap > biggest_gap:
biggest_gap = gap
gap_start = left
gap_end = right
print("Biggest gap:", biggest_gap)
print("It is between", gap_start, "and", gap_end)
```
Sorting Superpower
What you can do now
You now know that sorting helps data tell clearer stories.
Sorting can help you find:
- Smallest and biggest values
- Repeated values
- Groups and clusters
- Big gaps
- Values that deserve a closer look
This connects beautifully to the earlier lessons:
- From Introduction to Numbers and Counting, you know values have order.
- From Introduction to Statistics - Using Bubbles, you know data can describe a group.
- From Telling the Story with Data, you know data needs explanation.
- From Exploring Data Using Colors and Shapes, you know features help describe things.
Sorting is one of the first moves a data scientist makes when meeting a new dataset.
Next, you will learn how data can be stored and organized in useful containers in Building Blocks of Data Structures.
