Skip to content

Instantly share code, notes, and snippets.

@imtrinity94
Created March 27, 2023 11:07
Show Gist options
  • Save imtrinity94/ae49f511f7ef9560ac0baa3ac89fddd5 to your computer and use it in GitHub Desktop.
Save imtrinity94/ae49f511f7ef9560ac0baa3ac89fddd5 to your computer and use it in GitHub Desktop.
IP Sorter for vRO
// 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