How Do You Sort a Long List of IP Addresses in the Right Numeric Order?

IP address sorting — Chunky Munster

IP address sorting only works when each octet is treated as a number, not a string. That’s how 10.0.0.2 lands before 10.0.0.15 instead of getting buried under it like a bad log parser. If you want to clean up a pasted list fast, try our free IP address sorter.

Why plain text sorting breaks on IPs

A normal text sort compares characters from left to right. That’s fine for names and headings, but it fails on dotted decimal addresses because 192.168.1.100 is not “larger” than 192.168.1.20 just because 1 comes before 2 in a string comparison.

The bug is simple: string sorting is blind to numeric value. It sees 100 as three characters and 20 as two, then stops there. In network work, that can turn a clean firewall list, host inventory, or access log into a misleading mess.

This shows up anywhere IPs are copied and pasted by humans: CSV exports, incident notes, cloud security rules, DNS spreadsheets, and terminal output. The source data might already be valid, but the order is wrong for anything operational. A neat list is not the same thing as a useful one.

What numeric sorting actually means

Numeric sorting reads each IPv4 address as four integers separated by dots. It compares the first octet first, then the second, then the third, then the fourth. That means 10.2.0.1 comes before 10.10.0.1, because 2 is less than 10 when treated as a number.

The same rule applies when two addresses share a prefix. If you sort 172.16.0.9, 172.16.0.10, and 172.16.1.1, the tool should compare left to right at the octet level, not the character level. That’s the difference between actual network order and alphabet soup.

If you want to see how IPs are represented under the hood, our guide on how IPv4 addresses are represented in binary is a good companion read. It’s the same address, just with less human-friendly decoration.

Use cases where the right order matters

There’s a practical reason people care about this. Sorted IP lists are easier to scan for gaps, duplicates, ranges, and odd outliers. That matters when you’re reviewing access logs or checking whether a subnet has an address that doesn’t belong.

Here are a few places numeric order saves time:

In a sorted list, patterns jump out. You can spot 10.0.4.255 followed by 10.0.6.1 and immediately ask what happened to the 10.0.5.0/24 block. That kind of small visual check is hard to do when the list is sorted as plain text.

How the sorter handles messy input

Real-world lists are rarely pristine. You may have extra spaces, blank lines, repeated addresses, or labels like web01 10.0.0.12 pasted next to the IP. The first job is to isolate the addresses themselves before sorting, or the noise gets treated like data.

A good browser-based sorter is handy here because you can paste raw text directly into it. If your list includes only IPs, the process is trivial. If it includes other text, clean it first with a text tool, then sort the remaining addresses.

For example, a line-oriented cleanup pass might look like this:

web01 10.0.0.12
web02 10.0.0.3
10.0.0.19   staging

10.0.0.2

After stripping the labels, the actual IP list is what gets sorted. That avoids the classic mistake of ranking lines by whatever random word happened to come first.

IPv4 is straightforward; IPv6 is a different beast

This article is about IPv4 dotted-decimal addresses, which are usually what people mean when they say “IP addresses” in a spreadsheet or log file. IPv4 sorting is simple because each address has four numeric octets and a standard visual form.

IPv6 addresses are longer, use hexadecimal, and include compression rules like ::. Sorting those correctly is a different problem entirely, because string order and address order diverge even faster. If you’re working with IPv6, make sure the tool explicitly supports it before trusting the result.

For mixed network data, keep the categories separate. Sorting IPv4 and IPv6 together in one blob usually produces output that looks orderly and means almost nothing.

When to sort in a script instead

If you need this once, a browser tool is usually enough. If you need it repeatedly in a pipeline, it may be better to sort in code so the step is reproducible. That way the same logic can run against exports, test fixtures, or automation output without manual copy-paste.

In Python, for example, you can sort IPv4 addresses numerically with the standard library:

from ipaddress import ip_address

ips = [
    "10.0.0.15",
    "10.0.0.2",
    "192.168.1.100",
    "192.168.1.20",
]

print(sorted(ips, key=ip_address))

That works because ip_address converts each string into a real address object before sorting. In shell scripts or ad hoc data work, though, a browser sorter is faster than dragging in a parsing library for one cleanup pass.

See It in Action

Here’s the difference between text sorting and numeric IP address sorting with a small realistic list. The input is messy enough to show the failure mode clearly.

10.0.0.9
10.0.0.15
10.0.0.2
10.0.0.100
10.0.0.20

A plain text sort often produces something like this:

10.0.0.100
10.0.0.15
10.0.0.2
10.0.0.20
10.0.0.9

That order is useless if you are checking host ranges or reading logs. The numeric sort should give you this instead:

10.0.0.2
10.0.0.9
10.0.0.15
10.0.0.20
10.0.0.100

Now the list follows actual address order. If you were scanning for missing hosts, duplicate entries, or odd assignments, this is the version you want on screen.

Frequently Asked Questions

Why does alphabetical sorting put 192.168.1.100 before 192.168.1.20?

Because alphabetical sorting compares characters, not numbers. It sees 1 in 100 before 2 in 20, so the shorter number loses even though it is smaller numerically. That’s why IPs need numeric-aware sorting.

Can I sort IP addresses in Excel or Google Sheets?

Yes, but only if the values are in a format the spreadsheet understands. If the cells are plain text, the sort may be lexical instead of numeric, which gives the wrong order. Browser tools are often simpler when you’re dealing with pasted network data.

Does this work for IPv6 addresses too?

Not always. IPv6 uses hexadecimal and shorthand notation, so sorting needs proper address parsing, not just string comparison. Check that the tool or script explicitly supports IPv6 before relying on it.

Should I remove duplicates before sorting IPs?

Usually, yes, if your goal is to review unique addresses. Duplicates can hide patterns and make long lists harder to audit. If you need to preserve every line exactly as it appeared in the source, sort first and deduplicate only if that’s part of the task.

The Bottom Line

The core rule is simple: IP address sorting has to compare numbers, not characters. Once you do that, 10.0.0.2, 10.0.0.15, and 10.0.0.100 land where your brain expects them to land, and the list becomes readable again.

If you’re cleaning up a one-off export, pasted log block, or a messy allowlist, keep it boring and fast. Strip any non-IP noise, sort numerically, and check the result for duplicates or obvious gaps. When you want a quick pass in the browser, use the IP address sorter tool.

That’s usually enough to turn a wall of addresses into something you can actually work with. The network doesn’t care about aesthetics, but your future self probably does.

// try the tool
try our free IP address sorter →
// related reading
← all posts