Sorting Data

This chapter starts by looking at two background basics: the sorting order Numbers uses for letters, numbers, and blank cells; and Special Sorting Issues—how sorting interacts with headers, footers, merged cells, and hidden rows.

Next, I show you how to get Instant Sort Gratification using a table’s Column menu, and how to do more-sophisticated sorting in Sorting with the Inspector. Finally, you’ll learn how to Unsort Your Data and even Sort Unsortables.

Sorting Order

If you think sorting order is simple and obvious, then you need more variety in your sheets. Here’s how Numbers determines sorting order:

Special Sorting Issues

My job is not only to explain the things you want to know, but also the things you didn’t know you wanted to know. This roundup is from the latter category:

Instant Sort Gratification

Instant sorts are a cinch. With the table active or selected, open any Column menu, and choose Sort Ascending or Sort Descending to sort all the rows in the table (Figure 77) by that column.

**Figure 77:** The Column menu’s sort commands.
Figure 77: The Column menu’s sort commands.

You can subsort using Column menus, too (although you can do more sophisticated subsorts; see Sorting with the Inspector, just ahead).

To subsort a table, start with the least important column and work your way up to the most important one. So, in the example table, for a final sort order of Region:Team:Name, you’d first sort by Name, then by Team, and then by Region (Figure 78).

**Figure 78:** Left: The table sorted by Name. Center: A subsequent sort by Team. Right: A final sort by Region.
Figure 78: Left: The table sorted by Name. Center: A subsequent sort by Team. Right: A final sort by Region.

Sorting with the Inspector

Instant sorting isn’t your only option; you can also sort through the Sort & Filter Inspector. The simplest sort you can set up in the Inspector is a straightforward ascending or descending sort for the whole table based on one column. Sound familiar? Yes, that’s what you do when you instant-sort from a Column menu. But with the Sort & Filter Inspector, you can also:

Sort Criteria

Setting up sort criteria in the Sort & Filter Inspector is a easy: specify a column and whether you want it sorted in ascending or descending order. With your table active or selected:

  1. Go to the Sort & Filter Inspector’s Sort pane.
  2. Click Add a Column and choose a column from its pop-up menu (Region, in the example table).

    If the table doesn’t have a header row, or the header cell is blank, the columns are listed by their labels—Column A, Column B, and so on.

The table is immediately sorted by the default Ascending, and a new section appears in the Sort pane defining the criterion you just created: one pop-up menu identifies the Sort By column and the other specifies an ascending or descending sort (Figure 79).

**Figure 79:** Left to right: The data as entered, the criterion in the Sort & Filter Inspector, and the sorted result.
Figure 79: Left to right: The data as entered, the criterion in the Sort & Filter Inspector, and the sorted result.

For subsorts, define additional criteria in the Sort pane:

  1. Click Add a Column again and choose another column. Once again, the sort is done instantly. (In your example table, choose Team, and the data is sorted by Region with a subsort by Team.)
  2. Add another subsort by once again clicking Add a Column and choosing another column. (In the example table, choose Name, as shown in Figure 80, for a final sort order of Region:Team:Name.)
**Figure 80:** Left: The result of a sort by Region with a subsort by Team. Center: The three criteria. Right: The result of all three sort criteria.
Figure 80: Left: The result of a sort by Region with a subsort by Team. Center: The three criteria. Right: The result of all three sort criteria.

Edit Sort Criteria

It’s easy to alter the criteria in the Sort & Filter Inspector’s Sort pane:

**Figure 81:** Left: Sorted by Region, with a subsort by Team. Middle: Reordering criteria by dragging. Right: After the swap, Team is the primary sort.
Figure 81: Left: Sorted by Region, with a subsort by Team. Middle: Reordering criteria by dragging. Right: After the swap, Team is the primary sort.

The Sort Now Button

If you’ve been following along in this section, or have used the Sort & Filter Inspector before, you may have noticed that the Big Blue Button—that is, the Sort Now button—seems extraneous, since all the sorts were done automatically as you created the sort criteria.

When you edit the sorted data, you’ll need the Sort Now button to trigger an updated sort. Open the Sort & Filter Inspector’s Sort pane and click the button.

Special Solutions for Special Issues

Two sorting problems frequently rear their ugly little heads when you start sorting your data in various ways. One is when you want to return the data to the order it was in before you sorted it, but there’s no column that can sort it that way. The other is the need to sort by a column whose contents can’t be sorted “naturally,” such as grades that include A, A-, and A+.

Luckily, there are solutions for both situations coming right up.

Unsort Your Data

Hogwarts doesn’t have an Unsorting Hat, but you do, sort of: you can unsort your data if you plan ahead. To get back to a specific, yet not sortable, order (perhaps the order in which you entered your data, or maybe in a personal-preference order, as with categories in an expense table), you start by making a column that keeps track of how your rows were arranged before you do any sorting:

  1. Create a column at the end of the table with a click of the Column handle, and type Unsort in its header cell.
  2. Autofill the column with consecutive numbers starting at 1. (If you later add rows to your table, you’ll need to continue filling down the numbers in the Unsort column—after you unsort what’s already there so that the numbers will still be properly consecutive.)
  3. Hide the column by choosing Hide Column from its menu.

When you sort the visible columns in your table, the Unsort column keeps track of the original order (Figure 82).

**Figure 82:** Left: An Unsort column has been added to a table and filled with consecutive numbers. Right: The same table, after sorting and subsorting the data columns.
Figure 82: Left: An Unsort column has been added to a table and filled with consecutive numbers. Right: The same table, after sorting and subsorting the data columns.

To return the table to its unsorted state, you can:

Sort Unsortables

An “unsortable?” Say you have a table of student grades ranging from F to A+. You want to sort by grade, but Numbers’ default sort order is A, A-, A+ and so on, which is not very helpful. Or you want to sort your baseball card collection according to the condition of each item, which can range from Excellent through Very Good, Good, Poor and Very Poor. Imagine sorting that alphabetically instead of logically!

Even the example I used in this chapter for subsorts could be problematic in real life: the colors sort only alphabetically—Blue, Green, Red. If team colors or other elements in a table have some ordered meaning in the context of your data, you might need to sort them by importance.

These, and many other situations, call for hidden column sorting, similar to the “unsorting” column described just above. The difference is that the sort-by column you’re going to hide uses some formula to rank unsortable items instead of autofilled consecutive numbers.

There are many ways to assign numeric values to data in a table. Consider, for instance, the table used as an example in Instant Sort Gratification, earlier, where team names are Blue, Green, and Red and by default sort alphabetically. There are two easy ways you could assign a different sort order: