How to Sort Alphanumeric Codes Like A2 and A10 in Excel
By ExcelReady TeamUpdated 6 min read
The short answer
Excel sorts codes like A10 and A2 as text, one character at a time, so A10 comes first. To sort them naturally, split each code into its letters and its number: =LEFT(A2,1) and =VALUE(MID(A2,2,10)). Sort by the letters, then the number. In Excel 2021 or Microsoft 365, one SORTBY formula does both.
Key takeaways
- Codes such as A10 are text, and text sorts character by character, so A10 lands before A2.
- Split each code into a letter part with LEFT and a number part with VALUE(MID(...)).
- Sort by the letter column first and the number column second, with Data > Sort and Add Level.
- =SORTBY(A2:A9,LEFT(A2:A9,1),1,VALUE(MID(A2:A9,2,10)),1) sorts naturally in one formula (Excel 2021 or Microsoft 365).
- For new codes, pad the numbers with zeros (A002, A010) so a plain sort works.
On this page+
To sort alphanumeric codes in Excel so that A2 comes before A10, you have to split each code into its letters and its number, then sort by both. Excel treats a code like A10 as text and compares it one character at a time, which puts A10 ahead of A2. This guide shows three fixes, from helper columns that work in every version to a one-cell SORTBY formula, for anyone sorting bin locations, room numbers, SKUs or invoice codes.
Why Excel sorts A10 before A2#
Excel sorts text from left to right, one character at a time, like a dictionary. It compares the first characters, and only looks at the next ones if those are the same. For A10 and A2, the A matches, then the 1 in A10 beats the 2 in A2, so A10 wins. Excel never sees "ten" and "two".
Here are eight warehouse bin codes in A2:A9, sorted A to Z with the normal sort button, next to the order you actually want:
| Excel's text sort | Natural order |
|---|---|
| A1 | A1 |
| A10 | A2 |
| A2 | A10 |
| A21 | A21 |
| B1 | B1 |
| B12 | B2 |
| B2 | B3 |
| B3 | B12 |
The data is fine; the sort is doing exactly what it should with text. The fix is to give Excel a real number to sort by. If a normal sort misbehaves for other reasons, such as blank rows or merged cells, see why Excel sort is not working.
Fix 1: Split the code with LEFT, MID and VALUE#
Helper columns work in every version of Excel. Add two columns next to the codes:
| Column | Header | Formula in row 2 | Result for A10 |
|---|---|---|---|
| B | Letter | =LEFT(A2,1) | A |
| C | Number | =VALUE(MID(A2,2,10)) | 10 |
LEFT(A2,1)takes the first character.MID(A2,2,10)starts at character 2 and takes up to 10 characters, which is "everything after the letter". MID simply stops at the end of the text if there are fewer than 10 characters.VALUEturns the text "10" into the number 10, so it sorts as a number.
Fill both formulas down. The LEFT, RIGHT, MID and LEN lesson covers these text functions, and the MID function guide and VALUE function guide explain each argument.
Now sort the table:
- Select the whole table, including the headers.
- Go to Data > Sort.
- Sort by Letter, A to Z.
- Click Add Level and sort by Number, Smallest to Largest.
- Click OK.
The codes are now in natural order: A1, A2, A10, A21, B1, B2, B3, B12. Sorting by two columns this way is covered in more depth in how to sort by multiple columns.
Fix 2: Sort alphanumeric codes with one SORTBY formula#
In Excel 2021 or Microsoft 365, SORTBY can sort by the same two keys without helper columns. In an empty cell, type:
=SORTBY(A2:A9,LEFT(A2:A9,1),1,VALUE(MID(A2:A9,2,10)),1)SORTBY takes the range to return, then pairs of "sort by this" and "which direction" (1 for ascending, -1 for descending). Here the first key is the letter and the second is the number. LEFT, MID and VALUE work on the whole range at once and pass a list of results to SORTBY.
The result spills down eight cells, in natural order. The original list stays as it is, and the sorted copy updates whenever the codes change. If the spill range is blocked, you'll see #SPILL!; clear the cells below the formula.
If every code has the same prefix, such as room numbers R1 to R12, you only need the number key: =SORTBY(A2:A7,VALUE(MID(A2:A7,2,10))). Plain SORT can't do this, because it sorts by the values themselves. See SORT and SORTBY explained and the SORT function guide for the differences, or practise SORT in a live spreadsheet.
Codes with longer or mixed prefixes#
The formulas above assume one letter at the front. Real codes are often messier, so change how you find the number:
| Codes look like | Number part | Letter part |
|---|---|---|
| INV-7, INV-12 | =VALUE(TEXTAFTER(A2,"-")) | =TEXTBEFORE(A2,"-") |
| AB12, C3, XYZ45 | =VALUE(MID(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789")),20)) | =LEFT(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))-1) |
| 10A, 2B | =VALUE(LEFT(A2,LEN(A2)-1)) | =RIGHT(A2,1) |
The middle row finds the position of the first digit. FIND looks for each digit 0 to 9 in the code, MIN takes the earliest one, and adding "0123456789" to the end makes sure every digit is found somewhere, so FIND never errors. For AB12 the first digit is at position 3, so the letters are AB and the number is 12.
TEXTAFTER and TEXTBEFORE need Microsoft 365 or Excel 2024. In older versions, use =VALUE(MID(A2,FIND("-",A2)+1,10)). Our guide to extracting numbers from text has more patterns.
Fix 3: Pad the numbers with zeros#
If you control the codes, the simplest long-term fix is a fixed width: A001, A002, A010. Padded codes sort correctly with the normal sort button, because every code has the same number of characters.
To build a padded sort key from existing codes, use TEXT:
=LEFT(A2,1)&TEXT(VALUE(MID(A2,2,10)),"000")This turns A2 into A002 and A10 into A010. Sort by this column instead of the original codes. Use enough zeros for your largest number: "000" covers up to 999.
Numbers stored as text#
Sometimes the "codes" are only numbers that were saved as text, such as IDs imported from another system. When you sort a column like that, Excel may show a Sort Warning box. Choose Sort anything that looks like a number, as a number and the column sorts correctly.
This option only helps with pure numbers. It does nothing for A10 or B12, because those are not numbers at all. For mixed codes, use one of the fixes above.
Common mistakes#
- #VALUE! in the number column. One code has no digits after the letter, or has a space. Check it, or wrap the formula with IFERROR to push it to the end.
- Hidden spaces. "A10 " with a trailing space can break FIND and VALUE. Clean the column with TRIM first.
- Sorting only one column. If you select just the codes and sort, the rest of each row stays put. Select the whole table, or use the Sort dialog, which expands the selection.
- Helper columns that point at the wrong row. Use relative references (A2, not $A$2) so each row reads its own code.
To sort by a list you define, such as High, Medium, Low, see sorting in a custom order with a formula.
Practise it#
Try the exercise on this page: sort six room codes by their number with SORTBY. Then practise the SORT function in a live spreadsheet, and practise LEFT, RIGHT, MID and LEN to get quick at pulling numbers out of codes. Each lesson checks your formula instantly.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
The room codes in A2:A7 sort in the wrong order as text. In C2, return them sorted by their number, smallest first. Use SORTBY with VALUE and MID.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Sorted | |||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 |
Tip: MID(A2:A7,2,10) takes everything after the R. VALUE turns it into a number, and SORTBY uses those numbers to order the codes.
Practise this in a lesson
Frequently asked questions
Why does Excel sort 10 before 2?
Because the values are text, not numbers. Text is compared one character at a time, and the character 1 comes before 2, so 10 sorts first. Convert the numbers with VALUE, or split mixed codes into letters and a number, before sorting.
How do I sort numbers and letters together in Excel?
Add a helper column for the letters, =LEFT(A2,1), and one for the number, =VALUE(MID(A2,2,10)). Then use Data > Sort to sort by the letters and add a second level for the number.
Is there a natural sort in Excel?
There is no built-in natural sort option. You get the same result by sorting on helper columns that split the code, or with a SORTBY formula in Excel 2021 or Microsoft 365.
How do I sort codes with a dash, like INV-7 and INV-12?
Take the number after the dash with =VALUE(TEXTAFTER(A2,"-")) and sort by that column. TEXTAFTER needs Microsoft 365 or Excel 2024; in older versions use =VALUE(MID(A2,FIND("-",A2)+1,10)).