Krishna
Krishna

Reputation: 1985

Sort IP address column

I'm having one column named IPAddress in my table. I want to get IPAddress in sorted order.

IP Address:
8.123.10.124
192.23.34.45

If I use Order by IPAddress, i will get output as

192.23.34.45
8.123.10.124

Because order by sort it as string.

But I want output as

8.123.10.124
192.23.34.45

How to write a query for the same. Is there any way to split a string in HSQL

Upvotes: 1

Views: 143

Answers (1)

Vikas Godara
Vikas Godara

Reputation: 51

You first need to convert the IP Address from string to int. For example, IP address 1.1.1.1 would be 001001001001 you can use following logic

    while(ip[j]!='\0')
        {
         if(ip[j]!='.')
           a[i]=a[i]*10+ip[j]-'0';
         else
           i++;
         j++;
        }

After that you can multiple a[0],a[1].... by appropriate numbers (1e9,1e6,1e3,1e0) and add . However, you need to take some precaution such string which stores IP address doesn't have any white space saved with them and use unsigned long long int to store IP address in integer form

Upvotes: 1

Related Questions