CIDR Sorter
Sort a list of CIDR blocks numerically (IPv4/IPv6 aware)
Input CIDRs
Sorted Output
Sorted results will appear here
The CIDR Sorter arranges a list of CIDR blocks in proper numerical order, handling the complexities of IP address sorting that simple alphabetic sorting gets wrong. Paste a list like 192.168.1.0/24, 10.0.0.0/8, 172.16.0.0/16 and get back a correctly ordered result -- numerically sorted subnet lists make firewall rules, route tables, and network documentation significantly easier to read, audit, and maintain.
Understanding IP Address Sorting
Sorting IP addresses is deceptively tricky because they look like text but behave like numbers. If you paste a list of CIDR blocks into a spreadsheet and sort alphabetically, you get wrong results. The address 10.0.10.0 sorts before 10.0.2.0 because the character "1" comes before "2" in ASCII order. For single-octet comparisons this works fine -- 10.x.x.x correctly appears before 192.x.x.x -- but the moment any octet value exceeds 9, alphabetic sorting breaks down. The same problem affects every text-based tool, from sort on the command line to column sorting in a wiki or documentation system.
Proper numerical sorting treats each IP address as a single 32-bit (or 128-bit for IPv6) integer. The address 10.0.2.0 converts to the integer 167,772,672, while 10.0.10.0 converts to 167,774,720. Comparing these integers correctly places 10.0.2.0 first. This is the only reliable way to sort IP addresses, and it is what this tool does internally for every entry in your list.
The sorting gets more nuanced when multiple CIDRs share the same network address but have different prefix lengths. For example, 10.0.0.0/8, 10.0.0.0/16, and 10.0.0.0/24 all start at the same IP. The sorter uses the prefix length as a secondary sort key, placing shorter prefixes (larger blocks) before longer prefixes (smaller blocks) in ascending order. This mirrors the hierarchical structure of network allocations and makes the parent-child relationship between subnets immediately visible in the sorted output.
How to Sort CIDR Blocks Numerically
- Paste your list of CIDR blocks into the input field (one per line, comma-separated, or space-separated).
- Select your preferred sort order: ascending or descending.
- The tool sorts by network address first, then by prefix length for identical network addresses.
- Copy the sorted output for use in your configurations or documentation.
CIDR Sorting Examples
Why alphabetic sorting fails for IP addresses
Standard text sorting treats each character independently, producing incorrect ordering:
| Alphabetic Sort (wrong) | Numerical Sort (correct) |
|---|---|
10.0.0.0/24 | 10.0.0.0/24 |
10.0.0.0/8 | 10.0.0.0/8 |
10.0.10.0/24 | 10.0.1.0/24 |
10.0.1.0/24 | 10.0.2.0/24 |
10.0.2.0/24 | 10.0.10.0/24 |
192.168.1.0/24 | 172.16.0.0/16 |
172.16.0.0/16 | 192.168.0.0/24 |
192.168.0.0/24 | 192.168.1.0/24 |
Alphabetically, 10.0.10.0 sorts before 10.0.2.0 because the character 1 comes before 2. Numerical sorting correctly places 10.0.2.0 first because 2 < 10.
Sort order for same network, different prefix
When multiple CIDRs share the same network address, they are sorted by prefix length:
Input: Ascending output:
10.0.0.0/16 10.0.0.0/8
10.0.0.0/8 10.0.0.0/16
10.0.0.0/24 10.0.0.0/24
The /8 (broadest) appears first in ascending order because it represents the largest address block.
When to Use the CIDR Sorter
- Firewall rule maintenance -- sorted rule sets are easier to scan for gaps, overlaps, or missing entries than randomly ordered lists.
- Route table documentation -- presenting routes in numerical order helps network engineers quickly locate specific prefixes.
- Configuration management -- sorted CIDR lists produce cleaner diffs in version control, reducing noise in pull requests.
- Compliance audits -- auditors reviewing network segmentation can more efficiently verify coverage when subnets are logically ordered.
- IPAM cleanup -- sorting exported subnet lists reveals allocation patterns and makes gaps in address space visually obvious.
How Numerical IP Sorting Works
The sorter converts each IP address to its numeric value for comparison, avoiding the pitfalls of string-based sorting.
IPv4 sorting algorithm
Each IPv4 address is converted to a 32-bit unsigned integer:
192.168.1.0 = (192 * 16777216) + (168 * 65536) + (1 * 256) + 0
= 3232235776
10.0.2.0 = (10 * 16777216) + (0 * 65536) + (2 * 256) + 0
= 167772672
Comparing 167772672 < 3232235776 correctly places 10.0.2.0 before 192.168.1.0.
Mixed IPv4 and IPv6 handling
The sorter groups addresses by address family. IPv4 CIDRs are sorted together and IPv6 CIDRs are sorted together, with IPv4 appearing before IPv6 in the output (or vice versa, depending on your preference). Within each family, the same numerical comparison logic applies -- IPv6 uses 128-bit integer comparison.
Secondary sort by prefix length
When two CIDRs have the same network address, the prefix length determines order. In ascending mode, shorter prefixes (larger blocks) appear first. This mirrors how routing tables prioritize longest-prefix match but displays the hierarchy from broad to specific.
Sorting Tips for Clean Network Documentation
The biggest benefit of sorted CIDR lists is not the sorting itself but what it reveals about your address plan. When you sort a list of all your subnets numerically, gaps and inefficiencies become immediately visible. You might notice a jump from 10.0.3.0/24 to 10.0.8.0/24, revealing that blocks 4 through 7 are either unallocated or undocumented. You might spot two entries that are adjacent and could be aggregated into a single larger block. These patterns are invisible in an unsorted list but obvious in a sorted one.
For configuration management, consistency in sort order pays off every time you review a change. If your firewall rules are always sorted numerically, a diff between two versions of the configuration will only show the actual changes -- added or removed rules -- rather than a mix of changes and reordering noise. This makes code reviews faster and reduces the chance of missing a meaningful change buried in formatting differences.
A practical workflow for maintaining clean CIDR lists is to normalize first (using the CIDR Normalizer), then sort, then deduplicate (using the CIDR Deduper). This three-step pipeline produces a minimal, canonical, ordered list from any messy input. Automating this pipeline as part of your configuration management process ensures that your subnet documentation stays clean over time without manual effort.
Frequently Asked Questions
Does the sorter normalize CIDRs before sorting?
The sort is performed on the addresses as provided. For best results, run your list through the CIDR Normalizer first so that entries like 10.0.0.5/8 and 10.0.0.0/8 are treated consistently.
Can I sort a mixed list of individual IPs and CIDR blocks?
Individual IP addresses can be treated as /32 (IPv4) or /128 (IPv6) CIDRs. If your list contains bare IPs without a prefix length, add /32 or /128 to include them in the sort.
What input formats are accepted? The tool accepts CIDRs separated by newlines, commas, spaces, or semicolons. Leading and trailing whitespace is trimmed from each entry.
How are duplicate entries handled during sorting? Duplicates are preserved in the sorted output -- the sorter reorders but does not deduplicate. To remove duplicates, use the CIDR Deduper after sorting.
Can I reverse the sort order? Yes. The tool supports both ascending (smallest addresses first) and descending (largest addresses first) sort order. Ascending is the most common choice for documentation, while descending can be useful for specific reporting requirements.
How does sorting handle IPv6 addresses? IPv6 addresses are converted to 128-bit integers and sorted using the same numerical comparison as IPv4. Within a mixed list, IPv4 and IPv6 addresses are grouped by address family and sorted independently within each group.
Related Tools
- CIDR Normalizer - Normalize CIDR notation to canonical form before sorting
- CIDR Deduper - Remove duplicate and contained CIDR blocks from a sorted list
- Subnet Overlap Checker (List vs List) - Find overlapping subnets between two lists
- CIDR Aggregator - Aggregate multiple CIDR blocks into summarized routes
- Normalize Mixed Lists - Sort and clean mixed IPv4/IPv6 address lists