# How to sort by a custom order with a formula in Excel

Sort by each value's position in your own list: `=SORTBY(A2:C10,XMATCH(B2:B10,F2:F4))` puts High first, then Medium, then Low.

**Excel:** `=SORTBY(A2:C10,XMATCH(B2:B10,F2:F4))` (Excel 2021, 2024 and Microsoft 365)

**Google Sheets:** `=SORT(A2:C10,MATCH(B2:B10,F2:F4,0),TRUE)`

## Example

Sample data (row 1 is the header):

| A | B | C | D | E | F |
| --- | --- | --- | --- | --- | --- |
| Ticket | Priority | Opened |  |  | Order |
| T-301 | Low | 2026-05-02 |  |  | High |
| T-302 | High | 2026-05-03 |  |  | Medium |
| T-303 | Medium | 2026-05-03 |  |  | Low |
| T-304 | Low | 2026-05-05 |  |  |  |
| T-305 | High | 2026-05-06 |  |  |  |
| T-306 | Medium | 2026-05-07 |  |  |  |
| T-307 | Medium | 2026-05-08 |  |  |  |
| T-308 | High | 2026-05-09 |  |  |  |
| T-309 | Low | 2026-05-10 |  |  |  |

Formula in H2: `=SORTBY(A2:C10,XMATCH(B2:B10,F2:F4))`

Result (spills 9 × 3): T-302, High, 5/3/2026; T-305, High, 5/6/2026; T-308, High, 5/9/2026; T-303, Medium, 5/3/2026; T-306, Medium, 5/7/2026; T-307, Medium, 5/8/2026; T-301, Low, 5/2/2026; T-304, Low, 5/5/2026; T-309, Low, 5/10/2026

## How it works

XMATCH returns where each ticket's priority sits in the list `F2:F4`: 1 for High, 2 for Medium, 3 for Low. SORTBY sorts the table by those numbers, smallest first. Tickets with the same priority keep their original order, which here is oldest first.

Reorder the list in `F2:F4` and the table follows. Google Sheets' SORT takes the MATCH positions as its sort column.

## Other ways

- `=SORTBY(A2:C10,XMATCH(B2:B10,F2:F4),1,C2:C10,-1)`: Add a second key to sort within each priority, here newest first. (Newest ticket first inside each priority.)
- `=SORTBY(A2:C10,XMATCH(B2:B10,{"High","Medium","Low"}))`: Type the order into the formula when there's nowhere to keep a list.

## Common mistakes

- A value missing from the order list makes XMATCH return #N/A, and SORTBY fails. Add every value to the list, or wrap XMATCH in IFNA with a large number to send unknowns to the bottom.
- Sorting the priority column alphabetically gives High, Low, Medium, which is why a plain SORT doesn't work here.

## Questions

**How do I sort weekdays or month names in calendar order?**

Use the same pattern with the names in order: `=SORTBY(A2:C10,XMATCH(B2:B10,{"Mon","Tue","Wed","Thu","Fri"}))`.

**Can I do this without a formula?**

Yes: Data > Sort > Order > Custom List. The formula version updates on its own and leaves the source untouched.

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-a-custom-order
