How Data Actually Works

Asking questions data can answer

Sorting: putting rows in order

Sorting puts whole rows in order by one column, so the biggest or smallest values rise to the top.

By the end of this lesson you can sort a table to find the largest and smallest values.

Why it matters

"What was our biggest order?" "Which pupil travels furthest?" Questions about the biggest and smallest come up every day. With a sort, the answer jumps to the top of the screen.

The idea in plain words

Think of pupils lining up by height for a class photo. Each pupil moves as a whole person: their name, their uniform and their smile all move together. Nobody swaps heads.

Sorting a table works the same way. You pick one column to sort by, and whole rows move up or down together.

Sort: put rows in order by the values in one column. A to Z means smallest first (or alphabetical). Z to A means biggest first.

In Google Sheets, there are three steps:

Freezing row 1 first keeps the column names at the top, so they are not sorted in with the data.

See it done

Question: what was the biggest single sale at Amara's stall this week, by number of items?

Data: Amara's tab, market-stall-week.csv (synthetic).

  1. Plan. No conditions: every row counts. The answer column is quantity (column F). Biggest first means Z to A.
  2. Check row 1 is frozen. You did this in lesson 1.5 (View, Freeze, 1 row).
  3. Right-click the letter F at the very top of the quantity column.
  4. Click "Sort sheet Z to A".
  5. Read row 2. It is now sale S036: 12 oranges, on Thursday. That is the biggest single sale.
  6. Look at the next rows. Quantities 11, 11, 10, 10, 9 follow. Only one sale reached 12.

Whole rows moved. Row 2 is now the biggest sale of the week.

  1. Put it back. To return to time order, right-click the letter A (sale_id) and choose Sort sheet A to Z. Because the sale ids go S001, S002 ... in time order, this restores the original order.

Tip: Google Sheets also has Data, Sort range, Advanced range sorting options, where you tick Data has header row. Use it when you only want to sort part of a sheet.

Your turn, with help

Go to Lucía's tab. Which pupil takes the longest to get to school, and how many minutes?

Hint: the answer column is travel_minutes (G). Longest means biggest first: Z to A.

Show the answer

Pupil 4192 (class 5B), 40 minutes. After sorting column G Z to A, it is in row 2. Sort column A (pupil_number) A to Z afterwards to put the survey back in order.

Your turn, alone

  1. In Lucía's tab, which pupil has the shortest trip, and how long is it?
  2. In Amara's tab, sort by price_each_naira Z to A. Which fruit is the most expensive one to buy?
  3. In Amara's tab, sort by quantity A to Z (smallest first). What is the smallest quantity, and does more than one sale have it?
  4. Why must you freeze row 1 before using "Sort sheet"?
Check your answers
  1. Pupil 4182 (class 5B), 5 minutes.
  2. Watermelon, ₦3,500 each.
  3. The smallest is 1. Yes: 10 sales have a quantity of 1, so several rows tie at the top.
  4. Otherwise row 1, with the column names, gets sorted in with the data and may end up in the middle of the table.

Watch out

  • Sorting one column on its own. If you select only column F and sort just that, the quantities move but the fruits do not. Every row is then wrong. Always sort the whole sheet (or the whole table), so rows move together.
  • Forgetting the header. Freeze row 1 first, or tick Data has header row in the advanced sort.
  • Ties. When two rows have the same value, either may come first. Look at the next few rows, not just the top one.

Say it back

  1. When you sort a table, what moves?
  2. Which order puts the biggest numbers first?
  3. What was Amara's biggest single sale?
Show the answers
  1. Whole rows, together.
  2. Z to A.
  3. Sale S036: 12 oranges.

What is next

Sorting moves rows around, but every row is still there. What if you only want to see the pawpaw sales, and hide all the rest for a moment? Next lesson.

Enjoying the free course?

Join the list and we will email you when new courses and launch offers go live. No spam.