Sort the given IP addresses in ascending order
Last Updated :
12 Apr, 2023
Given an array arr[] of IP Addresses where each element is a IPv4 Address, the task is to sort the given IP addresses in increasing order.
Examples:
Input: arr[] = {‘126.255.255.255’, ‘169.255.0.0’, ‘169.253.255.255’}
Output: ‘126.255.255.255’, ‘169.253.255.255’, ‘169.255.0.0’
Explanation:
As the second octet of the third IP Address is less than the second IP Address whereas the first octet is same. Due to which the third IP Address will be smaller than the Third IP Address.
169.255.0.0 > 169.253.255.255
Input: arr[] = {‘192.168.0.1’, ‘192.168.1.210’, ‘192.168.0.227’}
Output: ‘192.168.0.1’, ‘192.168.0.227’, ‘192.168.1.210’
Approach: The idea is to use a custom comparator to sort the given IP addresses. Since IPv4 has 4 octets, we will compare the addresses octet by octet.
- Check the first octet of the IP Address, If the first address has a greater first octet, then return True to swap the IP address, otherwise, return False.
- If the first octet of the IP Addresses is same then compare the second octet of both IP Address. If the first address has a greater second octet, then return True to swap the IP address, otherwise, return False.
- If the second octet of the IP Addresses is also the same, then compare the third octet of both IP Address. If the first address has a greater third octet, then return True to swap the IP address, otherwise, return False.
- If the third octet of the IP Addresses is also the same, then compare the fourth octet of both IP Address. If the first address has a greater third octet, then return True to swap the IP address, otherwise, return False.
- If the fourth octet is also the same, then both IP Address is equal. Then also return False.
Below is the implementation of the above approach:
Java
import java.util.Arrays;
import java.util.Comparator;
public class GFG {
static class CustomComparator implements Comparator<String> {
@Override
public int compare(String a, String b) {
String[] octetsA = a.trim().split( "\\." );
String[] octetsB = b.trim().split( "\\." );
if (Arrays.equals(octetsA, octetsB)) {
return 0 ;
} else if (Integer.parseInt(octetsA[ 0 ]) > Integer.parseInt(octetsB[ 0 ])) {
return 1 ;
} else if (Integer.parseInt(octetsA[ 0 ]) < Integer.parseInt(octetsB[ 0 ])) {
return - 1 ;
} else if (Integer.parseInt(octetsA[ 1 ]) > Integer.parseInt(octetsB[ 1 ])) {
return 1 ;
} else if (Integer.parseInt(octetsA[ 1 ]) < Integer.parseInt(octetsB[ 1 ])) {
return - 1 ;
} else if (Integer.parseInt(octetsA[ 2 ]) > Integer.parseInt(octetsB[ 2 ])) {
return 1 ;
} else if (Integer.parseInt(octetsA[ 2 ]) < Integer.parseInt(octetsB[ 2 ])) {
return - 1 ;
} else if (Integer.parseInt(octetsA[ 3 ]) > Integer.parseInt(octetsB[ 3 ])) {
return 1 ;
} else {
return - 1 ;
}
}
}
public static void sortIPAddress(String[] arr) {
Arrays.sort(arr, new CustomComparator());
System.out.println(String.join( " " , arr));
}
public static void main(String[] args) {
String[] arr = { "192.168.0.1" , "192.168.1.210" , "192.168.0.227" };
sortIPAddress(arr);
}
}
|
Python
from functools import cmp_to_key
def customComparator(a, b):
octetsA = a.strip().split( "." )
octetsB = b.strip().split( "." )
if octetsA = = octetsB:
return 0
elif octetsA[ 0 ] > octetsB[ 0 ]:
return 1
elif octetsA[ 0 ] < octetsB[ 0 ]:
return - 1
elif octetsA[ 1 ] > octetsB[ 1 ]:
return 1
elif octetsA[ 1 ] < octetsB[ 1 ]:
return - 1
elif octetsA[ 2 ] > octetsB[ 2 ]:
return 1
elif octetsA[ 2 ] < octetsB[ 2 ]:
return - 1
elif octetsA[ 3 ] > octetsB[ 3 ]:
return 1
elif octetsA[ 3 ] < octetsB[ 3 ]:
return - 1
def sortIPAddress(arr):
arr = sorted (arr, key = \
cmp_to_key(customComparator))
print ( * arr)
return arr
if __name__ = = "__main__" :
arr = [ '192.168.0.1' ,
'192.168.1.210' ,
'192.168.0.227' ]
sortIPAddress(arr)
|
C#
using System;
using System.Linq;
public class SortIPAddress
{
public static int CustomComparator( string a, string b)
{
string [] octetsA = a.Trim().Split( '.' );
string [] octetsB = b.Trim().Split( '.' );
if (Enumerable.SequenceEqual(octetsA, octetsB))
{
return 0;
}
else if (Int32.Parse(octetsA[0]) > Int32.Parse(octetsB[0]))
{
return 1;
}
else if (Int32.Parse(octetsA[0]) < Int32.Parse(octetsB[0]))
{
return -1;
}
else if (Int32.Parse(octetsA[1]) > Int32.Parse(octetsB[1]))
{
return 1;
}
else if (Int32.Parse(octetsA[1]) < Int32.Parse(octetsB[1]))
{
return -1;
}
else if (Int32.Parse(octetsA[2]) > Int32.Parse(octetsB[2]))
{
return 1;
}
else if (Int32.Parse(octetsA[2]) < Int32.Parse(octetsB[2]))
{
return -1;
}
else if (Int32.Parse(octetsA[3]) > Int32.Parse(octetsB[3]))
{
return 1;
}
else
{
return -1;
}
}
public static void sortIPAddress( string [] arr)
{
Array.Sort(arr, CustomComparator);
Console.WriteLine( string .Join( " " , arr));
}
public static void Main( string [] args)
{
string [] arr = { "192.168.0.1" , "192.168.1.210" , "192.168.0.227" };
sortIPAddress(arr);
}
}
|
Javascript
<script>
function customComparator(a, b){
let octetsA = a.split( "." );
let octetsB = b.split( "." );
if (octetsA == octetsB)
return 0
else if (octetsA[0] > octetsB[0])
return 1
else if (octetsA[0] < octetsB[0])
return -1
else if (octetsA[1] > octetsB[1])
return 1
else if (octetsA[1] < octetsB[1])
return -1
else if (octetsA[2] > octetsB[2])
return 1
else if (octetsA[2] < octetsB[2])
return -1
else if (octetsA[3] > octetsB[3])
return 1
else if (octetsA[3] < octetsB[3])
return -1
}
function sortIPAddress(arr){
arr.sort(customComparator);
arr.forEach( function (ele){
document.write(ele + " " );
})
return arr;
}
arr = [ '192.168.0.1' , '192.168.1.210' , '192.168.0.227' ]
arr = sortIPAddress(arr)
</script>
|
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
bool customComparator(string a, string b) {
vector<string> octetsA;
vector<string> octetsB;
string octet = "" ;
for ( int i = 0; i < a.size(); i++) {
if (a[i] == '.' ) {
octetsA.push_back(octet);
octet = "" ;
} else {
octet += a[i];
}
}
octetsA.push_back(octet);
octet = "" ;
for ( int i = 0; i < b.size(); i++) {
if (b[i] == '.' ) {
octetsB.push_back(octet);
octet = "" ;
} else {
octet += b[i];
}
}
octetsB.push_back(octet);
if (octetsA == octetsB) {
return false ;
}
for ( int i = 0; i < 4; i++) {
if (stoi(octetsA[i]) > stoi(octetsB[i])) {
return false ;
} else if (stoi(octetsA[i]) < stoi(octetsB[i])) {
return true ;
}
}
return false ;
}
vector<string> sortIPAddress(vector<string> arr) {
sort(arr.begin(), arr.end(), customComparator);
return arr;
}
int main() {
vector<string> arr = { "192.168.0.1" , "192.168.1.210" , "192.168.0.227" };
arr = sortIPAddress(arr);
for ( int i = 0; i < arr.size(); i++) {
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}
|
Output:
192.168.0.1
192.168.0.227
192.168.1.210
Time Complexity: O(N*logN)
Space Complexity: O(N) as octetsA, octetsB vectors has been created.
Share your thoughts in the comments
Please Login to comment...