search
HomeTopicsexcelHow to sort mixed numbers and text, multilevel numbers in Excel

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

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

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

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)) *1

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

    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

  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

    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

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

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

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

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

The result is exactly what we were looking for:

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

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

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:

Custom function to sort hierarchy numbers
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 Index End 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

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

And get the multi-level numeric strings sorted as numbers:

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

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

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

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

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

As a result, the strings containing multi-level numbers are sorted by numbers:

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

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)

The above is the detailed content of How to sort mixed numbers and text, multilevel numbers in Excel. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MEDIAN formula in Excel - practical examplesMEDIAN formula in Excel - practical examplesApr 11, 2025 pm 12:08 PM

This tutorial explains how to calculate the median of numerical data in Excel using the MEDIAN function. The median, a key measure of central tendency, identifies the middle value in a dataset, offering a more robust representation of central tenden

Google Spreadsheet COUNTIF function with formula examplesGoogle Spreadsheet COUNTIF function with formula examplesApr 11, 2025 pm 12:03 PM

Master Google Sheets COUNTIF: A Comprehensive Guide This guide explores the versatile COUNTIF function in Google Sheets, demonstrating its applications beyond simple cell counting. We'll cover various scenarios, from exact and partial matches to han

Excel shared workbook: How to share Excel file for multiple usersExcel shared workbook: How to share Excel file for multiple usersApr 11, 2025 am 11:58 AM

This tutorial provides a comprehensive guide to sharing Excel workbooks, covering various methods, access control, and conflict resolution. Modern Excel versions (2010, 2013, 2016, and later) simplify collaborative editing, eliminating the need to m

How to convert Excel to JPG - save .xls or .xlsx as image fileHow to convert Excel to JPG - save .xls or .xlsx as image fileApr 11, 2025 am 11:31 AM

This tutorial explores various methods for converting .xls files to .jpg images, encompassing both built-in Windows tools and free online converters. Need to create a presentation, share spreadsheet data securely, or design a document? Converting yo

Excel names and named ranges: how to define and use in formulasExcel names and named ranges: how to define and use in formulasApr 11, 2025 am 11:13 AM

This tutorial clarifies the function of Excel names and demonstrates how to define names for cells, ranges, constants, or formulas. It also covers editing, filtering, and deleting defined names. Excel names, while incredibly useful, are often overlo

Standard deviation Excel: functions and formula examplesStandard deviation Excel: functions and formula examplesApr 11, 2025 am 11:01 AM

This tutorial clarifies the distinction between standard deviation and standard error of the mean, guiding you on the optimal Excel functions for standard deviation calculations. In descriptive statistics, the mean and standard deviation are intrinsi

Square root in Excel: SQRT function and other waysSquare root in Excel: SQRT function and other waysApr 11, 2025 am 10:34 AM

This Excel tutorial demonstrates how to calculate square roots and nth roots. Finding the square root is a common mathematical operation, and Excel offers several methods. Methods for Calculating Square Roots in Excel: Using the SQRT Function: The

Google Sheets basics: Learn how to work with Google SpreadsheetsGoogle Sheets basics: Learn how to work with Google SpreadsheetsApr 11, 2025 am 10:23 AM

Unlock the Power of Google Sheets: A Beginner's Guide This tutorial introduces the fundamentals of Google Sheets, a powerful and versatile alternative to MS Excel. Learn how to effortlessly manage spreadsheets, leverage key features, and collaborate

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor