ExcelReady
CourseMock testsFunction guidesBlog
Start freeStart learning for free
ExcelReady

Be Excel-ready before day one. Hands-on practice in your browser, with instant feedback.

Learn

  • Course
  • Mock tests
  • Function guides
  • Blog

Test prep

  • Excel level test
  • Excel test practice
  • Free screening quiz
  • Certificate

Company

  • About
  • Editorial policy
  • Contact
  • Terms
  • Privacy

© 2026 ExcelReady. All rights reserved.

  1. Blog
  2. Dynamic arrays
  3. How to Sort Alphanumeric Codes Like A2 and A10 in Excel

How to Sort Alphanumeric Codes Like A2 and A10 in Excel

By ExcelReady Team·Updated November 3, 2026·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+
  1. Why Excel sorts A10 before A2
  2. Fix 1: Split the code with LEFT, MID and VALUE
  3. Fix 2: Sort alphanumeric codes with one SORTBY formula
  4. Codes with longer or mixed prefixes
  5. Fix 3: Pad the numbers with zeros
  6. Numbers stored as text
  7. Common mistakes
  8. Practise it

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 sortNatural order
A1A1
A10A2
A2A10
A21A21
B1B1
B12B2
B2B3
B3B12

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:

ColumnHeaderFormula in row 2Result for A10
BLetter=LEFT(A2,1)A
CNumber=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.
  • VALUE turns 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:

  1. Select the whole table, including the headers.
  2. Go to Data > Sort.
  3. Sort by Letter, A to Z.
  4. Click Add Level and sort by Number, Smallest to Largest.
  5. 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:

Excel formula
=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 likeNumber partLetter 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:

Excel formula
=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.

fx
ABCD
1
Room
Floor
Sorted
2
R12
3
3
R2
1
4
R7
2
5
R10
3
6
R1
1
7
R3
1
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

LessonSORT: order data with a formulaSort a table by any column, ascending or descending, and combine SORT with FILTER and UNIQUE.LessonLEFT, RIGHT, MID and LENSplit product codes and IDs into their parts by taking characters from the start, end or middle of the text.Function guideSORTSort a range with a formula using Excel's SORT. Sort by any column, highest first, or combined with FILTER. Includes a live exercise.Function guideVALUEFix numbers stored as text with Excel's VALUE function, so SUM and lookups work again. Examples, common mistakes and a live practice exercise.Function guideMIDPull characters from any position with Excel's MID. Extract IDs, email domains and text between brackets with FIND. Examples and live practice.

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)).

Sources

  • SORTBY function - Microsoft Support
  • MID, MIDB functions - Microsoft Support
  • VALUE function - Microsoft Support

Written and checked by the ExcelReady Team. First published November 3, 2026. The practice exercise runs in the same formula engine as our lessons. Found a mistake? Read our editorial policy or email hello@excelready.app.

On this page

  1. Why Excel sorts A10 before A2
  2. Fix 1: Split the code with LEFT, MID and VALUE
  3. Fix 2: Sort alphanumeric codes with one SORTBY formula
  4. Codes with longer or mixed prefixes
  5. Fix 3: Pad the numbers with zeros
  6. Numbers stored as text
  7. Common mistakes
  8. Practise it

Keep reading

Dynamic arrays· 6 min read

Sort by Multiple Columns in Excel (Formula and Menu)

To sort by multiple columns in Excel, go to Data > Sort, pick the first column in Sort by, then click Add Level and pick the second column in Then by. For a live result, use a formula instead: =SORTBY(A2:C9,A2:A9,1,C2:C9,-1) sorts by column A from A to Z, then by column C from largest to smallest.

Text· 6 min read

Extract Numbers From Text in Excel

To extract a number from text in Excel, cut it out with a text function and convert it with VALUE. For a number after a dash, use =VALUE(RIGHT(A2,LEN(A2)-FIND("-",A2))), or =VALUE(TEXTAFTER(A2,"-")) in Microsoft 365. Use LEFT for numbers at the start and MID for numbers in the middle. VALUE turns the result into a real number.

Dynamic arrays· 6 min read

Sort by a Custom Order With a Formula

To sort by a custom list with a formula, use SORTBY with MATCH: =SORTBY(A2:C7,MATCH(B2:B7,E2:E4,0)). MATCH turns each value into its position in your list (High = 1, Medium = 2, Low = 3), and SORTBY orders the rows by those numbers. SORTBY needs Excel 2021 or Microsoft 365; older versions need a helper column.

More on dynamic arrays

  • Sort and Filter Together With One Formula
  • SORT and SORTBY in Excel
  • #SPILL! Error in Excel: What It Means and How to Fix It
  • The Spill Range Operator (#) in Excel
  • How to Sum Unique Values in Excel With UNIQUE
  • TAKE Function in Excel (and DROP): First or Last N Rows
All dynamic arrays articles →

Don't just read about Excel. Do it.

Practise real formulas in a live spreadsheet with instant feedback, right in your browser. Nothing to download.

Start learning for free