Sorting list of IP addresses
Posted on 2003-11-24
I am extremely surprised to not find a standard IP address sorting routine in Delphi. I search high and low
and couldn't find one so I started writing one myself. I found a couple written in Perl, but I'm definately
not THAT much of a geek and they used many built in Perl functions not avail in Delphi. I also found an
insertion sort routine in C that used inet_addr from the winsock library and figured I could do something
like that in Delphi.
Anyway...I came up something but it doesn't work correctly all the time and I can not understand why.
Here is a shortened copy that only compares two IP addresses and returns the larger of the two.
function CompareIPs(IP1, IP2: String):String;
a, b: TInAddr;
Result := '';
a.S_addr := inet_addr(PChar(IP1));
b.S_addr := inet_addr(PChar(IP2));
if a.S_addr > b.S_addr then
Result := inet_ntoa(a)
Result := inet_ntoa(b);
Lable1.Caption := CompareIPs('22.214.171.124', '126.96.36.199');
returns the 188.8.131.52 address. Go figure...
Can someone help me figure out what the hell is going on here and why it is returning
the wrong result, or am I just wrong in assuming that it is returning the wrong result.