Created
March 27, 2023 11:07
-
-
Save imtrinity94/ae49f511f7ef9560ac0baa3ac89fddd5 to your computer and use it in GitHub Desktop.
IP Sorter for vRO
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
// the array of the IP Address | |
// Custom Comparator to sort the | |
// Array in the increasing order | |
function customComparator(a, b){ | |
// Breaking into the octets | |
var octetsA = a.split("."); | |
var octetsB = b.split("."); | |
// Condition if the IP Address | |
// is same then return 0 | |
if(octetsA == octetsB) | |
return 0 | |
else if(parseInt(octetsA[0]) > parseInt(octetsB[0])) | |
return 1 | |
else if(parseInt(octetsA[0]) < parseInt(octetsB[0])) | |
return -1 | |
else if(parseInt(octetsA[1]) > parseInt(octetsB[1])) | |
return 1 | |
else if(parseInt(octetsA[1]) < parseInt(octetsB[1])) | |
return -1 | |
else if(parseInt(octetsA[2]) > parseInt(octetsB[2])) | |
return 1 | |
else if(parseInt(octetsA[2]) < parseInt(octetsB[2])) | |
return -1 | |
else if(parseInt(octetsA[3]) > parseInt(octetsB[3])) | |
return 1 | |
else if(parseInt(octetsA[3]) < parseInt(octetsB[3])) | |
return -1 | |
} | |
// Function to sort the IP Addresses | |
function sortIPAddress(arr){ | |
// Sort the Array using | |
// Custom Comparator | |
arr.sort(customComparator); | |
arr.forEach(function(ele){ | |
System.log(ele + " "); | |
}) | |
return arr; | |
} | |
// Driver Code | |
arr = ['192.168.20.1', '192.168.100.210', '192.168.0.227'] | |
// Function Call | |
arr = sortIPAddress(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment