Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort longest prefix #253

Open
mmihir82 opened this issue Apr 20, 2023 · 1 comment
Open

Sort longest prefix #253

mmihir82 opened this issue Apr 20, 2023 · 1 comment

Comments

@mmihir82
Copy link

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')]

@shepherdjay
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants