# How to sort by two columns with a formula in Excel

Give SORT arrays of columns and orders: `=SORT(A2:C9,{2,3},{1,-1})` sorts by department A to Z, then by salary from highest within each department.

**Excel:** `=SORT(A2:C9,{2,3},{1,-1})` (Excel 2021, 2024 and Microsoft 365)

**Google Sheets:** `=SORT(A2:C9,2,TRUE,3,FALSE)`

## Example

Sample data (row 1 is the header):

| A | B | C | D | E |
| --- | --- | --- | --- | --- |
| Employee | Department | Salary |  | Sorted |
| Ana Ruiz | Finance | 68500 |  |  |
| Raj Patel | Sales | 72000 |  |  |
| Lee Chen | IT | 81000 |  |  |
| Kim Ortiz | Sales | 69500 |  |  |
| Mo Diaz | Finance | 74000 |  |  |
| Sam Cole | IT | 88500 |  |  |
| Eve Lund | Sales | 77250 |  |  |
| Noor Ali | Finance | 61200 |  |  |

Formula in E2: `=SORT(A2:C9,{2,3},{1,-1})`

Result (spills 8 × 3): Mo Diaz, Finance, $74,000; Ana Ruiz, Finance, $68,500; Noor Ali, Finance, $61,200; Sam Cole, IT, $88,500; Lee Chen, IT, $81,000; Eve Lund, Sales, $77,250; Raj Patel, Sales, $72,000; Kim Ortiz, Sales, $69,500

## How it works

The second argument lists the columns to sort by, in order of priority: column 2 (department) first, then column 3 (salary) to break ties. The third argument gives an order for each: 1 for ascending, -1 for descending.

Google Sheets' SORT takes pairs of arguments instead, column then TRUE or FALSE, so the Sheets version is `SORT(A2:C9,2,TRUE,3,FALSE)`.

## Other ways

- `=SORTBY(A2:C9,B2:B9,1,C2:C9,-1)`: SORTBY takes ranges instead of column numbers, so it keeps working if columns are inserted. Excel only.

## Common mistakes

- Each sort column needs its own order: `{2,3},-1` sorts both columns descending. Give an array of orders, `{1,-1}`.
- In Google Sheets, array constants like `{2,3}` don't work in SORT. Use the column, order pairs.

## Questions

**How do I sort by three columns?**

Add a third column and order: `=SORT(A2:D9,{2,3,1},{1,-1,1})`.

**How do I sort by a column I don't want to show?**

Use SORTBY with that column as the key: `=SORTBY(A2:A9,C2:C9,-1)` returns just the names, highest salary first.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/sort-by-two-columns-with-a-formula
