Related Articles

# Sort the given IP addresses in ascending order

• Last Updated : 28 Jun, 2021

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:

## Python

 `# Python implementation to sort``# the array of the IP Address` `from` `functools ``import` `cmp_to_key` `# Custom Comparator to sort the``# Array in the increasing order``def` `customComparator(a, b):``    ` `    ``# Breaking into the octets``    ``octetsA ``=` `a.strip().split(``"."``)``    ``octetsB ``=` `b.strip().split(``"."``)``    ` `    ``# Condition if the IP Address``    ``# is same then return 0``    ``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` `# Function to sort the IP Addresses``def` `sortIPAdddress(arr):``    ` `    ``# Sort the Array using``    ``# Custom Comparator``    ``arr ``=` `sorted``(arr, key ``=` `\``       ``cmp_to_key(customComparator))``       ` `    ``print``(``*``arr)``    ``return` `arr``    ` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``'192.168.0.1'``,``           ``'192.168.1.210'``,``           ``'192.168.0.227'``]``    ` `    ``# Function Call``    ``sortIPAdddress(arr)`
Output:
```192.168.0.1
192.168.0.227
192.168.1.210```

Time Complexity: O(N*logN)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up