Why lexicographic sorting is wrong here
An IPv4 address is four numbers that happen to be written with dots between them. Sorting the text compares them digit by digit, so 10.0.0.9 lands after 10.0.0.10 and any address starting with 9 lands after one starting with 100. The result looks sorted at a glance, which is what makes it dangerous.
Sorting on the 32-bit value fixes it, and has the useful side effect of putting addresses in the same block next to each other — so scanning a sorted list makes the ranges obvious.
What gets cleaned up
| Pasted | Read as | Why |
|---|---|---|
| "10.0.0.1", | 10.0.0.1 | CSV quoting and trailing comma |
| [203.0.113.7] | 203.0.113.7 | Bracketed log format |
| 198.51.100.22:8443 | 198.51.100.22 | host:port pair |
| 10.0.0.1 | 10.0.0.1 | Repeats counted, kept once |
| 10.0.0.0/24 | unrecognised | A block, not an address |
| 2001:db8::1 | unrecognised | IPv4 only, for now |