How to Sort Excel Text by Leading Digits and Suffixes
Question details
The user wants to sort alphanumeric values in Excel based on their leading numeric digits.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Sorting a column containing a mix of numbers with leading zeros (e.g., 0031, 0033) and alphanumeric strings (e.g., 0031A, 0033A).
- Observed behavior
- Excel sorts the data incorrectly because it treats some entries as numerical values and others as text strings, breaking the desired logical order.
Identify whether your data has a consistent number of leading digits, as this will determine which formula or formatting method works best for your dataset.
Format Data as Text Before Entry
The simplest way to prevent mixed sorting issues is to ensure Excel treats all entries uniformly as text from the start.
When Excel mixes text strings and pure numbers, sorting behavior becomes unpredictable. By forcing all cells to be recognized as text, Excel applies alphabetical sorting rules uniformly, keeping leading zeros intact and maintaining logical sequence.
Highlight the entire column where you plan to enter the alphanumeric values.
Right-click the selection and choose 'Format Cells', or go to the Home tab and find the Number Format dropdown. Select 'Text' from the list of formats and click OK.
Alternatively, type an apostrophe (') before the number when entering data manually (e.g., '0031) to force Excel to store the value as text.

Extract and Sort Using the SORTBY Formula
If your data is already entered, use a formula to extract the leading digits and sort the array logically.
Standardize Text Formatting with SORT and TEXT
Use this method to pad numbers with leading zeros uniformly, ensuring text-based sorting orders them correctly.
Sort Complex Alphanumeric Data Easily in WPS Office
WPS Spreadsheet provides powerful data processing capabilities, including advanced sorting options and full support for dynamic array formulas like SORTBY. Manage your complex datasets efficiently and resolve mixed text formatting issues with ease.
- 1. Open your workbook: Launch WPS Spreadsheet and open the file containing your mixed alphanumeric entries.
- 2. Select the data range: Highlight the specific cells or column you wish to sort.
- 3. Access Custom Sort: Go to the 'Data' tab and click on 'Sort' to access custom sorting options, or format the cells as text directly from the Home tab.
- 4. Use dynamic array formulas: Alternatively, type the =SORTBY() formula directly into an empty cell to automatically generate a sorted list, just as you would in Excel.

Frequently Asked Questions
Why does Excel sort 0031 after 0031A?
Excel automatically removes leading zeros from numbers and treats them as purely numeric values (e.g., 31), while treating 0031A as a text string. Because Excel often sorts numbers differently than text, the logical alphanumeric sequence is broken.
How do I sort numbers and text together without using formulas?
You can use the 'Text to Columns' feature under the Data tab to split the numeric and alphabetical parts into separate columns. Sort the data by the numeric column first, and then optionally recombine them later.
What does the double dash (--) do in the SORTBY formula?
The double unary operator (--) converts the text string extracted by the LEFT function back into a true numeric value. This ensures the SORTBY function sorts the array numerically based on value rather than alphabetically.




