Skip to content

Instantly share code, notes, and snippets.

@elektrowolle
Created September 27, 2021 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elektrowolle/6f7fed19d14fd075fecd5e417e1496cc to your computer and use it in GitHub Desktop.
Save elektrowolle/6f7fed19d14fd075fecd5e417e1496cc to your computer and use it in GitHub Desktop.
const ips = [
"192.168.0.1",
"192.168.0.2",
"192.168.1.1",
"192.168.2.1",
"172.16.2.1",
"172.16.2.2",
"172.16.2.10",
"172.16.2.20",
]
const IPToNumber = (ip) => ip.split(".").reduce((a, v) => a * 255 + parseInt(v), 0);
const compareIPs = (ipA, ipB) => ipToNumber(ipA) > ipToNumber(ipB) ? 1 : -1;
const sortedIPs = ips.sort(compareIPs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment