ThisCalculator

Everyday

Sort by Two Columns? How It Works in Excel & Sheets

Updated · Standard formula, shown below

This guide explains how to sort data by two columns in spreadsheet programs like Microsoft Excel and Google Sheets, covering the exact steps, the logic behind multi-level sorting, and common pitfalls. You will learn how to prioritize one column over another, how to handle ties, and how to avoid the mistakes that scramble your data.

Why Sort by Two Columns at All

Sorting by a single column is straightforward: you alphabetize a list of names or order numbers from low to high. But real-world data rarely has a single unique key. Think of a class roster: many students share a last name, so sorting only by last name leaves the order of same-name students arbitrary. Sorting by two columns fixes that. The first column (the primary sort key) groups the data, and the second column (the secondary sort key) orders the rows within each group. This is often called a multi-level or nested sort.

Common use cases include:

  • Sorting sales records by region, then by sales amount within each region.
  • Sorting a product list by category, then by price within each category.
  • Sorting a task list by priority, then by due date.
  • Sorting a contact list by last name, then by first name.

How Multi-Level Sorting Works

When you apply a two-column sort, the program first arranges all rows by the primary column. Then, it looks at groups of rows that share the same value in the primary column and sorts those groups internally by the secondary column. The secondary column only affects the relative order of rows that are tied on the primary column. Rows in different primary groups never interleave.

For example, consider this unsorted data:

Name       Department   Salary
Alice      Sales        50000
Bob        Sales        60000
Carol      Marketing    55000
David      Marketing    52000

If you sort by Department (A-Z) and then by Salary (high to low), the result is:

Name       Department   Salary
Carol      Marketing    55000
David      Marketing    52000
Bob        Sales        60000
Alice      Sales        50000

The Marketing rows appear first because “Marketing” < "Sales". Within Marketing, Carol (55000) comes before David (52000) because salary is sorted descending. The same logic applies to Sales. If two rows have the same value in both columns, their relative order typically remains as it was in the original data (a stable sort), though not all programs guarantee this.

How to Sort by Two Columns in Excel

Microsoft Excel provides a dedicated dialog for multi-level sorting. The steps are:

  1. Select any cell inside your data range. Excel will automatically detect the range if it is contiguous.
  2. Go to the Data tab on the ribbon.
  3. Click Sort (not the A-Z or Z-A buttons, which only sort by one column).
  4. In the Sort dialog, the first row shows the primary sort level. Choose the column, sort on (values, cell color, font color, or icon), and order (A to Z, Z to A, or custom list).
  5. Click Add Level to add a second row.
  6. In the second row, choose the secondary column and its sort order.
  7. If you have a header row, ensure “My data has headers” is checked so the header is not sorted as data.
  8. Click OK.

You can add more than two levels if needed. The order of the levels in the dialog matters: the top row is the primary key, the next is secondary, and so on. You can reorder levels using the up/down arrows.

How to Sort by Two Columns in Google Sheets

Google Sheets offers a similar feature, though the interface is slightly different.

  1. Select the entire data range you want to sort, including the header row if present.
  2. Click Data in the menu, then Sort range.
  3. In the dialog that appears, check “Data has header row” if applicable.
  4. Click Add another sort column.
  5. For each sort column, choose the column and the order (A-Z or Z-A).
  6. Click Sort.

Alternatively, you can use the SORT function to create a sorted copy of your data without altering the original. The syntax is =SORT(range, sort_column1, is_ascending1, [sort_column2, is_ascending2, ...]). For example, =SORT(A2:C10, 2, TRUE, 3, FALSE) sorts by column B ascending, then by column C descending. This function is useful when you want to keep the original data intact and produce a sorted view elsewhere.

Common Mistakes and How to Avoid Them

Several errors frequently occur when sorting by multiple columns:

  • Not selecting the full range: If you select only one column and sort, the rest of the data stays in place, destroying the row integrity. Always select all columns or use the “Sort” dialog with an active cell inside the range.
  • Forgetting the header row: If you do not mark that your data has headers, the header row gets sorted as if it were a normal row, often ending up at the bottom.
  • Wrong level order: Placing the secondary column above the primary column in the dialog reverses the logic. The topmost level always takes precedence.
  • Mixing data types: If a column contains numbers stored as text (e.g., “10”, “9”, “8”), sorting alphabetically will produce 10, 8, 9 instead of 8, 9, 10. Ensure numeric columns are truly numeric.
  • Sorting only a subset of rows: If you have hidden rows or filtered data, the sort may only apply to visible rows, leaving hidden rows unsorted. Unfilter or unhide before sorting.

Sorting in Other Tools (SQL, Python, etc.)

Multi-column sorting is not limited to spreadsheets. In SQL, the ORDER BY clause accepts multiple keys: ORDER BY department ASC, salary DESC. In Python, the sorted() function or DataFrame.sort_values() in pandas allows a list of columns. The same principle applies: the first key is primary, the second is secondary. Understanding the concept in one tool makes it easy to transfer to another.

Frequently Asked Questions

Can I sort by more than two columns?

Yes. Both Excel and Google Sheets allow you to add any number of sort levels. The same logic extends: each additional level only breaks ties among rows that are equal on all previous levels. In Excel, click “Add Level” repeatedly. In Google Sheets, click “Add another sort column” repeatedly. The order of the levels determines the priority.

What is the difference between sorting and filtering?

Sorting rearranges the order of rows. Filtering hides rows that do not meet a condition. You can filter first and then sort the visible rows, which is often useful. In Excel, use the filter arrows on the header row and then choose a sort order from the dropdown. In Google Sheets, use the filter icon (funnel) and then the sort options in the column menu.

How do I sort by a custom order, like priority levels (High, Medium, Low)?

Both Excel and Google Sheets support custom sort orders. In Excel, go to the Sort dialog, choose “Custom List” in the Order dropdown, and define your list (e.g., High, Medium, Low) or import it from a range. In Google Sheets, you can create a custom list in the spreadsheet settings, but it is often easier to use a helper column with a numeric rank (High=1, Medium=2, Low=3) and sort by that column.

Why did my sort mess up my data?

This usually happens when you sort only a selected range of cells instead of the entire table. For example, if you select column B and sort, only column B moves, while the rest of the row stays put, scrambling your records. Always select all columns that belong to your data, or use the Sort dialog with a single active cell so the program expands the range automatically.

Can I sort by two columns without changing the original data?

Yes. In Google Sheets, use the SORT function to create a sorted copy elsewhere. In Excel, you can copy the data to a new sheet and sort the copy, or use the SORT function (available in Excel 365) which spills the sorted result into adjacent cells. This is helpful when you need to preserve the original order for other purposes.

Does sorting affect formulas or cell references?

Yes. If your formulas reference specific cells, sorting will move those cells and the references will follow the cell addresses, potentially changing the result. For example, if cell C2 contains =A2+B2, after sorting, the formula will still refer to the new A2 and B2, which may be different values. To avoid this, use absolute references (e.g., $A$2) only if you intend to keep the reference fixed, or use structured references like table names in Excel Tables.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.