How to Sort IP Addresses in Excel Correctly

📅 Mar 02, 2026 📝 Sarah Miller

Sorting IP addresses in Excel is notoriously frustrating, as standard sorting treats octets as text, incorrectly placing 10.0.0.1 before 2.0.0.1. While IT departments often rely on standard funding sources to procure enterprise network tools, many administrators must still manage raw data in spreadsheets. Fortunately, utilizing a helper column formula grants users the ability to organize network logs flawlessly without costly software.

As a stipulation, this method requires padding each octet with leading zeros to ensure proper alphanumeric alignment. This technique is highly effective when analyzing exports from systems like Cisco routers. Below, we break down the exact formulas needed to achieve perfect IP sorting.

How to Sort IP Addresses in Excel Correctly

If you have ever tried to sort a list of IP addresses in Microsoft Excel, you have likely run into a frustrating roadblock. Instead of sorting them in logical, numerical order (e.g., 1.1.1.1 followed by 2.2.2.2, and then 10.10.10.10), Excel sorts them alphabetically. This results in an incorrect order where 10.10.10.10 ranks higher than 2.2.2.2 because "1" comes before "2" in text sorting rules.

This happens because Excel treats IP addresses as text strings rather than numbers. An IPv4 address consists of four separate octets (numbers from 0 to 255) separated by periods. Because there are varying numbers of digits in each octet, standard alphabetical sorting algorithms fail to interpret their true numerical value.

In this comprehensive guide, we will explore the best methods to solve this problem, ranging from modern Excel 365 formulas to classic helper-column formulas, manual data manipulation, and Power Query.

Why Standard Excel Sorting Fails with IP Addresses

To understand why Excel struggles, look at how it compares text strings character by character from left to right:

  • 192.168.1.2
  • 192.168.1.20
  • 192.168.1.3

Alphabetically, Excel compares "2" and "3" in the last octet. But it also compares "20" and "3". Since "2" comes before "3", Excel puts 192.168.1.20 before 192.168.1.3. To sort them correctly, we must pad each octet with leading zeros so that every section contains exactly three digits (e.g., converting 192.168.1.3 to 192.168.001.003 and 192.168.1.20 to 192.168.001.020).


Method 1: The Modern Excel 365 Dynamic Formula (Recommended)

If you are using Microsoft 365 or Excel 2021, you have access to powerful new array manipulation functions like MAP, LAMBDA, TEXTSPLIT, and SORTBY. We can leverage these functions to sort your IP addresses dynamically in a single cell without altering your original data structure.

The Formula

Assuming your unsorted list of IP addresses is in the range A2:A11, enter the following formula in an empty cell (e.g., C2):

=SORTBY(A2:A11, MAP(A2:A11, LAMBDA(ip, CONCAT(TEXT(TEXTSPLIT(ip, "."), "000")))))

How It Works

This elegant formula executes the following sequence behind the scenes:

  1. TEXTSPLIT(ip, "."): Splits each individual IP address by the dot delimiter into an array of four separate numbers. For example, "192.168.1.5" becomes {"192", "168", "1", "5"}.
  2. TEXT(..., "000"): Formats each split number to ensure it has exactly three digits. This transforms our array into {"192", "168", "001", "005"}.
  3. CONCAT(...): Merges the padded array back into a single string: "192168001005".
  4. MAP(A2:A11, LAMBDA(...)): Loops this logic through every IP address in your designated range, generating a hidden array of 12-digit normalized strings.
  5. SORTBY(A2:A11, ...): Sorts your original IP list based on the alphabetical values of the newly generated 12-digit normalized strings. Because every octet is now of uniform length, alphabetical sorting perfectly mirrors true numerical sorting.

Method 2: The Classic Formula for Older Excel Versions (2019, 2016, and Earlier)

If you are working on an older version of Excel, you won't have access to dynamic array functions. Instead, you can use a helper column containing a formula that converts the IP addresses into a padded, sortable format, and then sort by that helper column.

The Helper Formula

Insert a column next to your IP addresses. Assuming your first IP address is in cell A2, paste this formula into B2 and drag it down:

=TEXT(TRIM(MID(SUBSTITUTE(A2,".",REPT(" ",100)),1,100)),"000")&"."&TEXT(TRIM(MID(SUBSTITUTE(A2,".",REPT(" ",100)),101,100)),"000")&"."&TEXT(TRIM(MID(SUBSTITUTE(A2,".",REPT(" ",100)),201,100)),"000")&"."&TEXT(TRIM(MID(SUBSTITUTE(A2,".",REPT(" ",100)),301,100)),"000")

How It Works

This formula may look intimidating, but it follows a highly consistent mathematical pattern:

  • SUBSTITUTE(A2, ".", REPT(" ", 100)): Replaces every dot in the IP address with 100 spaces. This spaces out the numbers widely, guaranteeing they won't overlap.
  • MID(..., [Start], 100): Isolates each block of text. It checks character positions 1 to 100 for the first octet, 101 to 200 for the second, 201 to 300 for the third, and 301 to 400 for the fourth octet.
  • TRIM(...): Removes all the excess trailing and leading spaces, leaving just the isolated string number (e.g., "2").
  • TEXT(..., "000"): Formats the isolated string into a 3-digit number (e.g., converting "2" to "002").
  • &"."&: Concatenates the padded octets back together with periods, yielding a perfectly formatted, sortable string: 192.168.001.002.

To Sort Your Data:

  1. Apply the formula down your entire helper column.
  2. Select your entire dataset, including the IP address and helper columns.
  3. Go to the Data tab on the Ribbon and click Sort.
  4. Choose to sort by your Helper Column in Ascending (A to Z) order.
  5. Your original IP addresses will now be perfectly sorted! You can hide the helper column to keep your sheet clean.

Method 3: The "Text to Columns" Sorting Technique (No Formulas)

If you need a quick, one-off sort and prefer not to write or copy complex formulas, you can temporarily split your IP addresses into four separate helper columns using Excel's built-in Text to Columns wizard.

Step-by-Step Instructions

  1. Duplicate your IP column: Insert four empty columns directly to the right of your IP address list. Copy and paste your IP address list into the first new helper column.
  2. Convert Text to Columns: Select the newly copied IP addresses. Go to the Data tab and click Text to Columns.
  3. Choose Delimited: Select Delimited and click Next.
  4. Select the Delimiter: Uncheck all options except Other, and type a period (.) in the text box. Click Next and then Finish. Your IPs will now be split into four columns containing the numeric octets.
  5. Perform a Multi-Level Sort:
    • Select your entire data range (including the original IP column and the four split columns).
    • Go to the Data tab and click Sort.
    • Add four levels of sorting, ordering sequentially from left to right: Sort by the first split column, then by the second, then the third, and finally the fourth. Set all options to sort Smallest to Largest.
  6. Clean Up: Once sorted, you can safely delete the four helper columns. Your original IP column will remain perfectly ordered.

Method 4: Sorting IP Addresses with Power Query

For large, recurring datasets (such as system logs or network inventories), Power Query is the most robust tool. Once set up, you can refresh your sorted list with a single click whenever new IP addresses are added.

Step-by-Step Power Query Process

  1. Select your table of IP addresses, go to the Data tab, and click From Table/Range to load your data into the Power Query Editor.
  2. Right-click the header of your IP column and select Duplicate Column.
  3. Select the duplicated column, go to the Transform tab, click Split Column, and select By Delimiter. Power Query should automatically detect the dot (.) delimiter. Click OK.
  4. You will now have four split columns. Select all four columns, right-click, and choose Change Type > Whole Number. (This ensures Excel sorts them numerically rather than alphabetically).
  5. Select the four split columns sequentially (from first to last), and click the Sort Ascending button on the Home tab.
  6. Once sorted, select the four helper columns, right-click, and select Remove Columns.
  7. Go to the Home tab and click Close & Load to return your perfectly sorted table back to Excel.

Method Comparison Summary

Method Best For... Dynamic? Difficulty
Excel 365 Dynamic Formula Modern Office users seeking a single-cell, automated solution. Yes (Automatic) Easy (Plug & Play)
Classic Padded Formula Legacy Excel versions (Excel 2019 and older). Yes (With helper column) Medium
Text to Columns Quick, one-time manual sorting without using formulas. No (Manual) Easy
Power Query Large, recurring server logs or continuous data updates. Yes (On refresh) Medium-High

By using any of the methods above, you can bypass Excel's alphabetical limitations and organize your IP addresses in correct, logical sequence. For most modern users, the Excel 365 Dynamic Formula is the cleanest and fastest path to success.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.