How to sort mixed numbers and text, multilevel numbers in Excel (2024)

The tutorial explains common problems with ordering alphanumeric strings in Excel and provides working solutions to sort numbers as text and text as numbers.

Sorting text and numbers separately in Excel is as easy as ABC or 123 :) But re-arranging product IDs, SKU's and other values that have both letters and digits in them may cause problems. Excel's alphabetical sort cannot distinguish a numeric part of a string to handle it separately. If you want to have more control over how alphanumeric stings are sorted, look through the examples below.

Sort numbers as text in Excel

When sorting a column of numbers from smallest to largest in Excel, instead of having this:

1, 2, 3, 11, 12, 13, 110, 120, 130

you may sometimes get something like this:

1, 11, 110, 12, 120, 13, 130, 2, 3

That happens when numbers are actually text and they sort as text - like words that are arranged based on their letters, "text-numbers" are sorted based on their digits instead of their values.

If the target column contains numbers formatted as text, all it takes for the numbers to sort normally is to convert text to number. The result is in column C in the screenshot below.How to sort mixed numbers and text, multilevel numbers in Excel (1)

On the contrary, if you wish to sort numbers as text, convert numbers to text first, and then click Home tab > Editing group > Sort & Filter > Sort A - Z.How to sort mixed numbers and text, multilevel numbers in Excel (2)

The result will be like in column A in the previous screenshot.

How to sort mixed numbers and text in Excel

With the built-in Excel Sort feature, alphanumeric strings (combined text and numbers) are always sorted as text, i.e. letter-by-letter, digit-by-digit. To sort a column of numbers containing a letter prefix or suffix as numbers, carry out the steps below. And here's the result we are trying to achieve.How to sort mixed numbers and text, multilevel numbers in Excel (3)

To prevent Excel from handling numbers in alphanumeric strings as text, we're going to extract those numbers in a helper column, and sort by that column. This will let you keep the original data unchanged but rearrange it the way you like.

  1. In a column next to the original values, enter a formula that extracts number from string.

    In this example, we can simply extract all the characters after a hyphen ("-"). In Excel 365, this can be done using the TEXTAFTER function. As with any other Text function, its output is always text, regardless of whether you are retrieving letters or digits, so we multiply the result by 1 to convert a text string to a number.

    =TEXTAFTER(A2, "-")*1

    In older Excel versions, the same result can be achieved using a combination of three different functions that extract text after a specific character. Additionally, the *1 operation is performed to convert the text output to number:

    =RIGHT(A2, LEN(A2) - SEARCH("-", A2)) *1How to sort mixed numbers and text, multilevel numbers in Excel (4)

    Tip. To quickly extract numbers from any position in a cell, you can use the Extract tool included with our Ultimate Suite for Excel.

    How to sort mixed numbers and text, multilevel numbers in Excel (5)
  2. Sort the original data by the extracted numbers. With the numbers selected, go to the Home tab > Sort & Filter > Sort Smallest to Largest. In the dialog box that pops up, choose the Expand the selection option and click Sort. For more details, see How to sort and keep rows together.How to sort mixed numbers and text, multilevel numbers in Excel (6)

    Now the strings containing letters and digits are sorted as numbers, and you can remove or hide the helper column if necessary.How to sort mixed numbers and text, multilevel numbers in Excel (7)

Sort strings by text and numbers at a time

In case your source strings have a few different elements, you can extract each element in a separate column, and then sort by multiple columns. As a result, the strings will be arranged like in column D in the screenshot below:How to sort mixed numbers and text, multilevel numbers in Excel (8)

As all the strings in our dataset have the same pattern, the easiest way to split text and numbers into different columns is by using regular expressions. This can be done with the help of the custom function named RegExpExtract. First, you add its code to your Excel as explained in this tutorial. After that, make use of the following formulas.

To extract the first occurrence of text from cell A2, the formula in B2 is:

=RegExpExtract(A2, "[^\d]+", 1)

To extract the first number, the formula in C2 is:

=RegExpExtract(A2, "\d+", 1)*1

To extract the second number, the formula in D2 is:

=RegExpExtract(A2, "\d+", 2)*1

In the 2nd and 3rd formulas, we multiply the RegExpExtract output by 1 to turn an extracted substring into a number.How to sort mixed numbers and text, multilevel numbers in Excel (9)

The users of our Ultimate Suite can leverage the Regex Tools to achieve the same result. They will just need to perform one more quick operation to convert the extracted numeric strings (columns C and D) to numbers:How to sort mixed numbers and text, multilevel numbers in Excel (10)

Once the text and numbers are split, sort your dataset by multiple columns:

  1. Select all the columns (A2:D16 in our case).
  2. On the Data tab, in the Sort & Filter group, click the Sort button.
  3. Add as many sort levels as you need and choose the desired sort order.
  4. When done, click OK.

In our case, the selected range is first sorted by Text from A to Z, then by Number 1, and then by Number 2 from smallest to largest:How to sort mixed numbers and text, multilevel numbers in Excel (11)

The result is exactly what we were looking for:How to sort mixed numbers and text, multilevel numbers in Excel (12)

Tip. To re-arrange the strings differently, change the order of levels in the Sort dialog box.

How to sort multilevel / hierarchy numbers in Excel

In Microsoft Excel, multilevel numbers such as 1.1, 1.1.1, 1.1.2 are strings, and they sort as text, not numbers:How to sort mixed numbers and text, multilevel numbers in Excel (13)

To sort multilevel strings as numbers, you can use a custom user-defined function that has the following syntax:

HierarchyNumber(range, del, max_level)

Where:

  • Range is the range to sort.
  • Del is the delimiter used for separating the levels, typically a point (.).
  • Max_level is the maximum number of levels in the hierarchy numbers.

And here is the function's code:

Function HierarchyNumber(range As range, del As String, max_level As Integer) As Double Dim part As Variant part = Split(range.Value, del) For Index = 0 To UBound(part) HierarchyNumber = HierarchyNumber + part(Index) * (10 ^ ((max_level - Index) * 2)) Next IndexEnd Function

What the function does is generate numbers that allow sorting multilevel numeric strings. More precisely, it splits a hierarchy string into individual numbers (parts), processes each part based on the below algorithm, and adds up the results:

part(Index) * (10 ^ ((level - Index) * 2))

For example, for 1.1, the function produces this result:

1*(10^((2-0)*2)) + 1*(10^((2-1)*2)) = 10100

For 1.2, it's:

1*(10^((2-0)*2)) + 2*(10^((2-1)*2)) = 10200

How to sort hierarchy numbers using the custom function:

  1. Insert the code of the HierarchyNumber function into a standard code module of your workbook and save it as a macro-enabled workbook (.xlsm). The detailed instructions are here.
  2. In a blank cell next to the first hierarchy string, enter a HierarchyNumber formula and press the Enter key.
  3. Drag the formula down across as many rows as needed.
  4. Sort your dataset based on the column of formulas.

Suppose you have multilevel numeric strings with a maximum of 3 levels in column A. The formula for B2 is:

=HierarchyNumber(A2, ".", 3)

After copying the formula down, you'll get this result:How to sort mixed numbers and text, multilevel numbers in Excel (14)

After that, you sort the whole dataset (A2:B19) by the formula column from smallest to largest:How to sort mixed numbers and text, multilevel numbers in Excel (15)

And get the multi-level numeric strings sorted as numbers:How to sort mixed numbers and text, multilevel numbers in Excel (16)

Sort multilevel numbers prefixed with text

This example discusses a slightly different case where multilevel numbers are prefixed with some text, say "Level 1.1.1". At first sight, the task sounds more complicated, but in fact the solution is simpler :)

First, you extract multilevel numeric substrings into a separate column. For this, you can use the Extract tool included with our Ultimate Suite:How to sort mixed numbers and text, multilevel numbers in Excel (17)

Or you can write a formula to pull all characters after a space:

=RIGHT(A2, LEN(A2) - SEARCH(" ", A2))How to sort mixed numbers and text, multilevel numbers in Excel (18)

And then, do custom sorting. In the Excel Sort dialog box, add two levels - first by the extracted hierarchy number, and then by the original string - and click OK:How to sort mixed numbers and text, multilevel numbers in Excel (19)

The following warning will pop up, where you choose to "Sort numbers and numbers stored as text separately".How to sort mixed numbers and text, multilevel numbers in Excel (20)

As a result, the strings containing multi-level numbers are sorted by numbers:How to sort mixed numbers and text, multilevel numbers in Excel (21)

That's how to sort numeric strings and multi-level numbers in Excel. Thank you for reading and see you on our blog next week!

Available downloads

Sort mixed numbers and text in Excel - examples (.xlsm file)
Ultimate Suite fully-functional trial version (.exe file)

You may also be interested in

  • Excel SORT function with examples
  • SORTBY function: custom sort with formula
  • How to sort by date in Excel
  • How to sort by color in Excel
  • How to alphabetize tabs in Excel
  • Random sort in Excel: shuffle cells, rows and columns
How to sort mixed numbers and text, multilevel numbers in Excel (2024)

FAQs

How to sort mixed numbers and text, multilevel numbers in Excel? ›

To sort, highlight both columns and sort on B. If there can be other prefixes, say b-, c-, etc., similarly split off the text portion to another column. Then sort on the text column as the first sort column and the number column as the second. Everything you highlight will get sorted according to the sort columns.

How do I sort numeric and alphanumeric Data in Excel? ›

To sort, highlight both columns and sort on B. If there can be other prefixes, say b-, c-, etc., similarly split off the text portion to another column. Then sort on the text column as the first sort column and the number column as the second. Everything you highlight will get sorted according to the sort columns.

How do I sort hierarchy numbers in Excel? ›

Sort data in a table
  1. Select Custom Sort.
  2. Select Add Level.
  3. For Column, select the column you want to Sort by from the drop-down, and then select the second column you Then by want to sort. ...
  4. For Sort On, select Values.
  5. For Order, select an option, like A to Z, Smallest to Largest, or Largest to Smallest.

How do you sort numbers with text prefix or suffix in Excel? ›

1. Select a blank cell beside the numbers with letter prefix or suffix, says Cell B2, type the formula =EXTRACTNUMBERS(A2,TRUE) (A2 is the cell of number with letter prefix or suffix) into it, and then drag this cell's AutoFill Handle to the range as you need.

Do alphanumeric order letters or numbers first? ›

Sorting alpha numeric strings

Any alpha numeric sort places the alpha in order, and then numbers in order by their first numeral, so 1300 will come before 140 which does not work well for lists like call numbers in libraries.

How do I sort odd and even alphanumeric in Excel? ›

Sort rows by odd or even numbers with a helper column
  1. Next to the numbers, please enter this formula =ISODD(A2) in a blank cell, see screenshot:
  2. Then drag the fill handle over to the cells that you want to contain this formula, the TRUE indicates odd numbers and FALSE indicates even numbers, see screenshot:

Does Excel sort numbers or letters first? ›

When you have a list of data that contains both letters and numbers, you might want to sort the data so that the numbers come before the letters. Excel does not offer a built-in option to sort numbers before letters, but you can use a custom sort order to achieve this.

Why is Excel not sorting numbers correctly? ›

There are many times in Excel that data is imported from the internet or from other programs into Excel. Often this data is not imported or copied into Excel in a consistent number format. If this is the case, it might make sorting numerically incorrect.

How do I sort mixed data in Excel? ›

Sort by more than one column or row
  1. Select any cell in the data range.
  2. On the Data tab, in the Sort & Filter group, click Sort.
  3. In the Sort dialog box, under Column, in the Sort by box, select the first column that you want to sort.
  4. Under Sort On, select the type of sort. ...
  5. Under Order, select how you want to sort.

How sorting with multiple fields work? ›

You can further sort the records in the bookstore data set by specifying multiple control fields. When you specify two or more control fields, you specify them in the order of greater to lesser priority. Note that control fields might overlap or be contained within other control fields.

How to do multilevel grouping in Excel? ›

Select the data (including any summary rows or columns). On the Data tab, in the Outline group, click Group > Group Rows or Group Columns. Optionally, if you want to outline an inner, nested group — select the rows or columns within the outlined data range, and repeat step 3.

How do I create a nested hierarchy in Excel? ›

Right-click the selected columns and select Create hierarchy. This creates a new parent hierarchy level below the table columns. Each column is now a child in the hierarchy. Type a name for the hierarchy and press ↵ Enter .

How do you create a hierarchical structure in Excel? ›

Create a hierarchy
  1. On the Insert tab, in the Illustrations group, click SmartArt.
  2. In the Choose a SmartArt Graphic gallery, click Hierarchy, and then double-click a hierarchy layout (such as Horizontal Hierarchy).
  3. To enter your text, do one of the following: Click [Text] in the Text pane, and then type your text.

How do you create a hierarchy structure in Excel? ›

On the Insert tab, in the Illustrations group, click SmartArt. In the Choose a SmartArt Graphic gallery, click Hierarchy, click an organization chart layout (such as Organization Chart), and then click OK.

What is alphanumeric sorting example? ›

A list of given strings is sorted in alphanumeric order or Dictionary Order. Like for these words: Apple, Book, Aim, they will be sorted as Aim, Apple, Book. If there are some numbers, they can be placed before the alphabetic strings.

What is alphanumeric sequencing? ›

Alphanumeric sequence is a sequence which consists of both alphabets and numbers. In this sequence, we can also add some symbols along with alphabets and numbers. For example we can make an alphanumeric sequence as follows − A $ E R 9 * T 5 F 6 @ D 8.

When alphabetizing do numbers go first or last? ›

Numbers do not go before letters in an MLA works cited. Numbers are listed in alphabetical order as if they were spelled out. So, an organization with a number like '24/7Service', would be alphabetized as if it said, 'twenty-four-seven service'.

How do you do alphanumeric sorting? ›

Alphanumeric ordering is done using the current language sort order on the client machine as defined by the operating system (i.e. Windows). The user requests the sort by clicking on the column header. A grid must have column headings in order to be sorted by the user.

How do I filter numbers by letters in Excel? ›

Filter a range of data
  1. Select any cell within the range.
  2. Select Data > Filter.
  3. Select the column header arrow .
  4. Select Text Filters or Number Filters, and then select a comparison, like Between.
  5. Enter the filter criteria and select OK.

How do I segregate numbers and alphabets in Excel? ›

Here are steps you can use to separate numbers from text in Excel using the "Text to Columns" tool:
  1. Select the cells. ...
  2. Locate the "Text to Columns" tool under the "Data" tab. ...
  3. Select the data type from the menu. ...
  4. Adjust the settings and options. ...
  5. Format and place your columns. ...
  6. Find the position of the number in the string.
Jun 24, 2022

How do you filter data begins with numbers or letters in Excel? ›

Filter a range of data
  1. Select any cell within the range.
  2. Select Data > Filter.
  3. Select the column header arrow .
  4. Select Text Filters or Number Filters, and then select a comparison, like Between.
  5. Enter the filter criteria and select OK.

How do you sort alphanumeric data? ›

Alphanumeric ordering is done using the current language sort order on the client machine as defined by the operating system (i.e. Windows). The user requests the sort by clicking on the column header. A grid must have column headings in order to be sorted by the user.

What data can Microsoft Excel sort numeric text alphanumeric? ›

Explanation: excel can sort all of them.

How do I separate numbers and text in Excel without delimiter? ›

How do I separate text in Excel without a delimiter? You can't. You need some kind of delimiter—whether that's another character such as a comma, period, semicolon, etc. or a fixed number of characters. This answer is NOT for reproduction in a paid space.

How do I separate text and decimal numbers in Excel? ›

Select a cell and type this formula =A1-TRUNC(A1) (A1 is the cell you want to extract decimal value from) into the Formula Bar, and then press Enter key. Keep selecting the first result cell, and drag fill handle down to get all results. You can see the decimal values are extracted with sign as below screenshot shown.

How do you separate numbers and letters in sheets? ›

Select the text or column, then click the Data menu and select Split text to columns... Google Sheets will open a small menu beside your text where you can select to split by comma, space, semicolon, period, or custom character. Select the delimiter your text uses, and Google Sheets will automatically split your text.

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5737

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.