• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Not correct IP sorting in DBGRID

Is it possible to show IP adresses in DBGRID in correct way ???
if i order by ip adress i get not correct sorting :/
*.*.6.10
*.*.6.101
*.*.6.11
*.*.6.12
*.*.6.13
0
selas
Asked:
selas
  • 5
  • 2
1 Solution
 
ziolkoCommented:
yup, but you have to sort them as integers

ip = a.b.c.d

int_ip = a*256^3 + b*256^2 + c*256^1 + d*256^0

ziolko.
0
 
Martin BarredaLic. in Computer ScienceCommented:
Another way is formating IP number as string of XXX.XXX.XXX.XXX... that is... each block has to have 3 digits... completed with leading zeros if you must.

speaker.
0
 
selasAuthor Commented:
How to do this with SQL ???
select * from table order by IP
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Martin BarredaLic. in Computer ScienceCommented:
IP field is an string field?
With which database are you working with

Looking for a solution under LocalSQL with BDE...

But, perhaps the best solution is to store the fields as a string just formated as i stated above (xxx.xxx.xxx.xxx). Then if you want to show ip numbers like 192.168.1.1 instead of 192.168.001.001 you can include a calculated field and reformat the string to its original value with Delphi function.
0
 
Martin BarredaLic. in Computer ScienceCommented:
You can also produce the calculated fied in SQL:

select trim(both ' ' from (cast(cast(substring(ipf from  1 for 3) as integer) as char(3)))) + '.' +
           trim(both ' ' from (cast(cast(substring(ipf from  5 for 3) as integer) as char(3)))) + '.' +
         trim(both ' ' from (cast(cast(substring(ipf from  9 for 3) as integer) as char(3)))) + '.' +
         trim(both ' ' from (cast(cast(substring(ipf from 13 for 3) as integer) as char(3)))) as ipnum, ip
from ipnumbers.db
order by ip

This suppose you have a table named IPNUMBERS.DB with a field named IP as Char(15).
Existing data:
192.168.001.010
192.168.001.101
192.168.001.011
192.168.001.001

The result should be (tested with Database Desktop with Paradox Table):

192.168.1.1
192.168.1.10
192.168.1.11
192.168.1.101
0
 
selasAuthor Commented:
yes IP is a string field
i use microsoft SQL server
and IP adresses stored as 192.168.1.15
0
 
Martin BarredaLic. in Computer ScienceCommented:
I do not know if you can in MS-SQL, but you may parse each part of IP string as an integer, then order as

select parsepart1asinteger as part1,
parsepart2asinteger as part2,
parsepart3asinteger as part3,
parsepart4asinteger as part4,
ip
from ipnumbers
order part1, part2, part3, part4

where parsepartNasinteger is a sql function like:

cast( substring(ip from N1 for N2) as integer)

where N1 and N2 has to be the value of the location of the separator point (except for N1 in first string and N2 in last string).

I really do not know if there is a MS-SQL function that do this work. Perhaps you have to ask this question in MS-SQL Server zone.

Hope this help!!!
0
 
Martin BarredaLic. in Computer ScienceCommented:
MsSQL sintax... probably :-)... can test it

select cast(substring(ip from 1 for charindex('.',ip)-1) as integer) as part1,
          cast(substring(ip from charindex('.',ip)+1 for charindex('.',ip, charindex('.',ip)+1) as integer) as part2,
          cast(substring(ip from charindex('.',ip, charindex('.',ip)+1)+1 for charindex('.',ip, charindex('.',ip, charindex('.',ip)+1)+1) as integer) as part3,
          cast(substring(ip from charindex('.',ip, charindex('.',ip, charindex('.',ip)+1)+1) +1 for charindex('.',ip, charindex('.',ip, charindex('.',ip,charindex('.',ip)+1)+1)+1) as integer) as part4, ip

from ipnumbers

order by part1, part2, part3, part4

This might work... Found examples of this in:

http://www.sqlbooks.ru/readarticle.aspx?part=09&file=addition09
or
http://www.databasejournal.com/features/mssql/article.php/3071531

Must work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now