0% found this document useful (0 votes)
199 views

Sorting IP Address

The document describes four methods for sorting IP addresses stored as strings: 1. Convert to a TreeMap but IP strings will sort incorrectly when compared digit-by-digit. 2. Convert each IP to a long integer by removing dots and adding leading zeros, sort the longs, then reconstruct the IP strings. 3. Use the GNU sort command with options to sort by dotted-decimal IP fields numerically. 4. Convert each IP to a long by shifting bytes and use in a TreeMap, sorting by the long keys to implicitly sort the IP string values.

Uploaded by

Ramya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views

Sorting IP Address

The document describes four methods for sorting IP addresses stored as strings: 1. Convert to a TreeMap but IP strings will sort incorrectly when compared digit-by-digit. 2. Convert each IP to a long integer by removing dots and adding leading zeros, sort the longs, then reconstruct the IP strings. 3. Use the GNU sort command with options to sort by dotted-decimal IP fields numerically. 4. Convert each IP to a long by shifting bytes and use in a TreeMap, sorting by the long keys to implicitly sort the IP string values.

Uploaded by

Ramya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assumptions: 

 
IP addresses as Strings are available as a HashMap 
Consider 32bit IP IN THE FORM OF MAX UPTO 255.255.255.255 
 
Method1: 
 
Convert the Hashmap to TreeMap. 
But , as the Ips are strings while sorting it will sort by each digit as a character. 
If we have ips 10.10.1.1 , 10.10.22.2, 10.10.10.1….It will sort as 
10.10.1.1,10.10.10.1,10.10.22.2 which is not valid 
 
Method2: 
 
Iterate over each IP, and convert each IP in​ x​ xx.xxx.xxx.xxx​ format by adding heading 
zeros. 
Now make a long int of the IP by removing (.) dot and parse to long int. 
Now take all these longints into a longint array and sort them using sort function. 
Now all the Ips in the array are sorted. 
If it is in list.. Sort it using  
Collections.sort(list_name); 
Now Add a (.) after each 3 digits of the longint and then convert it to string . 
If requirements are concerned with adding heading zeros, then before converting to 
string just remove any heading zeros and then convert to strng and add to a Map. 
Finally all the Ips in the map will be sorted. 
 
Method3: 
 
This method is specific to GNU. 
We have a command sort 
$ sort <filename> or <file path> 
But running the above command will generate same error as method 1. 
So we have a format for the 32bit IP as 
$ sort -n -t. -k 1,1 -k 2,2 -k 3,3 -k 4,4 <filename or filepath> 
 
I’ve got here a list of numbers (-n), but each item in the list consists of some 
subnumbers, fields set apart from the others by a dot (-t .). Sort first by the first field, 
and only the first field (-k 1,1), then by the second and only the second (-k 2,2), and so on 
(-k 3,3 -k 4,4). 
We can use it while we awk or grep like 
$ maprcli virtualip list |awk '{print $3}'| sort -n -t. -k 1,1 -k 2,2 -k 3,3 -k 4,4 
This will return a output in sorted order. 
 
Method4: 
 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.Map; 
import java.util.TreeMap; 
 
public class IPSort { 
private Map sorted = new TreeMap(); 
 
public void add(String ip) throws UnknownHostException { 
sorted.put(new Long(ipToLong(ip)), ip); 

 
private static long ipToLong(String ip) throws UnknownHostException { 
byte[] address = InetAddress.getByName(ip).getAddress(); 
long n = 0; 
for (int i = 0; i < address.length; i++) { 
long x = address[i]; 
if (x < 0) { 
x += 256; 

n += x << ((3 - i) * 8); 

System.out.println(n); 
return n; 

 
public static void main(String args[]) throws UnknownHostException { 
IPSort sortobject = new IPSort(); 
sortobject.add("206.113.192.12"); 
sortobject.add("12.180.165.21"); 
sortobject.add("12.180.165.23"); 
sortobject.add("138.108.218.106"); 
sortobject.add("216.52.132.10"); 
sortobject.add("138.108.218.105"); 
sortobject.add("12.180.165.22"); 
sortobject.add("138.108.222.157"); 
sortobject.add("10.10.111.22"); 
sortobject.add("10.10.11.122"); 
sortobject.add("10.10.2.2"); 
System.out.println(sortobject.sorted.values()); 


 
 
 
All the ips after going through n+=x<<((3-i)*8) are as follows: 
 
3463561228 
213165333 
213165335 
2322389610 
3627320330 
2322389609 
213165334 
2322390685 
168455958 
168430458 
168428034 
 
In the above program , the given 32bit IPs are stored as key:value pairs with key being 
the 32bit value where each 8bits are assigned for each section of ip and value is the IP. 
For Example: 10.20.3.4 --> the key is 10 is shifted 24 times to fit the last 32-25 bits, 20 is 
shifted 16 times and 3 is shifted 8 times to accommodate a 32bit long value. 
Then the map is sorted w.r.t keys 
And all the values are returned as an array. 
This is the easiest and smallest program. 
 
 
 

You might also like