Sorting IPv4 Addresses

Posted by Kumba on Server Fault See other posts from Server Fault or by Kumba
Published on 2011-01-12T10:49:09Z Indexed on 2011/01/12 10:54 UTC
Read the original article Hit count: 263

Filed under:
|

So I've run into a quandary on sorting IPv4 addresses, and didn't know if there was a set rule in some obscure networking document. Do I do a straight sort on the raw address only (such as converting the IP address to a 32bit number and then sorting), do I factor in the CIDR via some mathematical formula, do I sort via the CIDR only (as if I'm comparing the network size and not the addresses directly)?

I.e., normal math, we'd do something like -1 < 0 < 1 to denote the order of precedence. Given say, 10.1.0.0/16, 172.16.0.0/12, 192.168.1.0/24, and 192.168.1.42, what would be the order of precedence?

© Server Fault or respective owner

Related posts about ipv4

Related posts about sort