Sort longest prefix
mmihir82 opened this issue · comments
Hello, how to find longest prefix with the same network?
Can cidr_merge will work?
[IPNetwork('10.0.2.0/25'), IPNetwork('10.0.0.0/23'), IPNetwork('10.0.2.0/23'), IPNetwork('10.0.4.0/22')]
desired output would like to have
[IPNetwork('10.0.0.0/23'), IPNetwork('10.0.2.0/23'), IPNetwork('10.0.4.0/22')]
I know this is a much older issue so may not be relevant to you anymore. However Python supports passing a key function to sorted. So you can simply do this:
by_longest = sorted(networks, reverse=True, key=lambda network: network.prefixlen)