inetaf / netaddr

Network address types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add Less methods for some types?

josharian opened this issue · comments

I find myself needing to sort a slice of netaddr.IPPorts, in order to do an order-insensitive slice comparison.

It'd be nice to have IP.Less, IPPort.Less. (Alternatively, we could have Compare functions that return -1, 0, or 1. Since we have == well-defined, I think we may as well just do <.)

I propose that IP.Less would sort zero smallest, then IPv4, then IPv6. IPv4 sorts big-endianly (127.0.0.0 > 1.1.1.255). IPv6 sorts similarly, with zones as tie-breaker, sorted by regular string comparison.

I propose IPPort.Less would sort first by IP and then by port.

In a sorted list, this would organize IPPorts by address family, then by class A, etc, then by port, which seems pretty natural.


Alternative ways to solve my problem include:

  • Write this less code in situ.
  • Write an IPPortSet mirroring IPSet.
  • Call String on all my IPPorts and work with this.
  • Use a map. This allocates, but it's what I'm going to do in the interim, because it is easy and obvious.

Why was this closed? Do you no longer want IPPort.Less?

Yeah. I ended up using a map, and without an active need for Less, no reason to rush to add it.