Created
January 3, 2015 00:44
-
-
Save ryanmjacobs/b3eab5da2780c106a2a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* sort_ip.js | |
* | |
* run with: 'node ./sort_ip.js' | |
*/ | |
// compare function | |
var compare = function(a, b) { | |
var aa = a.split("."), | |
bb = b.split("."); | |
for (var i = 0; i < 4; i++) { | |
var aai = parseInt(aa[i]), | |
bbi = parseInt(bb[i]); | |
if (aai == bbi) continue; | |
if (aai < bbi) return -1; | |
else return 1; | |
} | |
return 0; | |
} | |
// not sorted | |
not_sorted = [ | |
"10.0.10.1", | |
"192.168.1.12", | |
"10.0.10.44", | |
"192.168.1.1", | |
"10.0.10.15", | |
"10.0.10.90", | |
"100.10.10.1", | |
"191.122.123.112", | |
"192.161.1.1", | |
"191.122.123.1", | |
"123.24.5.78", | |
"121.24.5.78", | |
"123.24.4.78", | |
"123.2.5.78", | |
"192.1.1.1", | |
"125.45.67.89", | |
"1.1.1.1", | |
"3.4.5.6", | |
"2.2.2.2", | |
"6.6.6.7", | |
"155.155.23.0" | |
]; | |
// sorted | |
sorted = [ | |
"1.1.1.1", | |
"2.2.2.2", | |
"3.4.5.6", | |
"6.6.6.7", | |
"10.0.10.1", | |
"10.0.10.15", | |
"10.0.10.44", | |
"10.0.10.90", | |
"100.10.10.1", | |
"121.24.5.78", | |
"123.2.5.78", | |
"123.24.4.78", | |
"123.24.5.78", | |
"125.45.67.89", | |
"155.155.23.0", | |
"191.122.123.1", | |
"191.122.123.112", | |
"192.1.1.1", | |
"192.161.1.1", | |
"192.168.1.1", | |
"192.168.1.12" | |
]; | |
// do it | |
not_sorted.sort(compare); | |
// check it | |
for (var i=0, len = sorted.length; i < len; i++) { | |
if (not_sorted[i] == sorted[i]) | |
console.log("GOOD : "+not_sorted[i]+" == "+sorted[i]) | |
else | |
console.log(" BAD : "+not_sorted[i]+" != "+sorted[i]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment