python3 -c 'import ipaddress, sys; print("\n".join(sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
It takes the IP addresses to be sorted on the command line.
Or, re-abbreviating them by removing zeroes and attempting to use :: where possible:
python3 -c 'import ipaddress, sys; print("\n".join(str(ipaddress.IPv6Address(y)) for y in sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
Both of these versions will crash if given input that isn't syntactically valid as an IPv6 address.
miyuru · 1h ago
I don't think any IP version was made in with sorting as a feature, its just a by product of the representation.
This post also highlights a major thing I discovered when deploying and using IPv6, which is that you don't "Lift and shift" IPv4 to IPv6.
This is one of the reason its hard to deploy, because people cannot use the same IPv4 concepts to IPv6. For unknown reason they do, they will find the same problem they had with v4.
ggm · 2h ago
for all addresses, v4 and v6 this is what I used to do
1) convert to a non-space-zeros-compressing hex string
2) sort on the hex string
3) convert back through inet_ntop()
Only a minor variant needed to deal with prefix/len sort order.
fragmede · 2h ago
It's ipv6. Seems like the "standard" thing to do would be to write and name a utility called sort6 which properly handles ipv6 addresses.
Gabrys1 · 1h ago
I imagine you wrote this sarcastically, but I do like this a lot!
wpm · 2h ago
Almost as if using hex values for IP addresses was a bad idea.
0points · 2h ago
1. Convert to numeric representation
2. Sort
3. Convert back
sargstuff · 3h ago
IPv6 addresses are typically written in canonical text representation recommended by RFC 5952[0].
1) Regular expression / seamingly lost art of sprintf formatting are some methods to normalize 001:db8::2:1 to something usable for sorting aka 2001:db8:0000:0000:0000:0000:0002:0001. Perhaps restoring to rfc 5952 format when printing sorted results.
2) Modify hex to 'sortable utf-16 characters', modify back post sort[1]
Or, re-abbreviating them by removing zeroes and attempting to use :: where possible:
Both of these versions will crash if given input that isn't syntactically valid as an IPv6 address.This post also highlights a major thing I discovered when deploying and using IPv6, which is that you don't "Lift and shift" IPv4 to IPv6.
This is one of the reason its hard to deploy, because people cannot use the same IPv4 concepts to IPv6. For unknown reason they do, they will find the same problem they had with v4.
2. Sort
3. Convert back
1) Regular expression / seamingly lost art of sprintf formatting are some methods to normalize 001:db8::2:1 to something usable for sorting aka 2001:db8:0000:0000:0000:0000:0002:0001. Perhaps restoring to rfc 5952 format when printing sorted results.
2) Modify hex to 'sortable utf-16 characters', modify back post sort[1]
3) avoid utf-8 / utf-16 issues, use relevant python libraries to handle ipv4/ipv6[2][3],[4]: ip2n < file | sort -n | n2ip
----------------------------
[0] : https://datatracker.ietf.org/doc/html/rfc5952
[1] : https://stackoverflow.com/questions/5797369/unix-sort-utilit...
[2] : https://stackoverflow.com/questions/75522231/how-to-sort-ipv...
[3] : https://www.geeksforgeeks.org/convert-ip-address-to-integer-...
[4] : https://ipfind.com/blog/how-to-convert-ip-address-to-integer...