[Tutor] IP sorting
Thu, 27 Dec 2001 15:23:06 -0500
> Yes, but it'll sort them in the "wrong" order--you want all *.com
> names to come
> before all *.net names (or so sayeth the spec given by Roman Suzi
> ;) ). In order to
> do this *just* for symbolic domain names, you could try this:
Okay, then plan 'B'. Instead of a simple > < comparison, use a function to
compare and have the function return a value based on a custom comparison
where it first compares the TLD then the actual address octets.
This is by no means a definitive means of doing it, but it works, I've done
it for similar data, yadda yadda. Basically, it's a bubble sort with a
custom comparison function. the variable 'Array' is all the elements that
you want to sort.
swap_flag = 1:
while swap_flag == 1:
for i in range(0,len(array)-1)
if compare(array(i), array(i+1)) == 1: # 1 means we want to swap
x = array(i)
array(i) = array(i+1)
array(i+1) = x
# first, split a and b into tuples using the split() function
# then compare the TLDs. I don't know what your criteria is here, but
# you should return '1' if you want to swap, and continue otherwise.
# then you iterate through the four octet values for the two elements.
# return '1' if you want to swap, else continue to the next octet
# until you're out of octets.
# if you get here, return '0'.
A good candidate for sorting octets would be:
for j in range(1,4): # assuming element 0 is the TLD and the
# remainder are the octets
if int(a[j]) > int(b[j]: # if we're sorting ascending, we need to swap
elif int(a[j]) < int(b[j]: # No need to swap, but don't continue
# if we got through all four iterations and still no difference, return
# 0 to indicate equality
Those familiar with such things will realize that this is a basic bubble
sort. I am not yet familiar enough with the deep gizzards of Python
libraries to know if there's an inbuilt sorting function that's better or
faster. If there's something like the ansi C lib's qsort(), the outline of
the compare() function would be perfect.
The above is suitable for an array of elements that are in the format
Obviously I haven't sussed the actual format quite yet :-)
> However, this still doesn't fulfill the original spec, which
> asked to sort a mixed
> list of domain names and numeric IP addresses... I suppose that
> one could do
> something like:
> def sort_domains_and_IPs(unsortedlist):
> list1, list2 = segregate_IPs(unsortedlist)
> list1 = sort_IPs(list1)
> list2 = sort_domains(list2)
> return list1
> wherein I assume that all numeric IP addresses should sort before
> all named domains
The above compare function could be altered to prioritiz the octets before
the tlds, of course (by detecting alpha characters), but another -- SLOW --
alternative might be to use Python's internet libraries to RESOLVE the TLD
addresses and then replace them in the array with thier octets. This is
incredibly slow for purposes of analyzing a log in real-time, but might be
well-suited for a background task that kicked off on a copy of the day's
*** or *** :-)
Create two lists, one with numerics and one without. Do two sorts, and
present them in TWO seperate outputs. If it were me, this is the way I would
do it because it would be far more readable (IMHO).
> This looks like it ought to work, but it's untested and strictly
> off the top of my
> head. It's also probably horribly inefficient. :)
Yeah, double for me :-) But if our primary concern was efficiency, a
different language might be more appropriate anyway, but if it goes along
smartly on my wimpy 400 MHz machine, I'm sure that the only bottleneck on a
more modern machine would be the file I/O anyway. :-)
Big megahertz are a wonderful thing :-D
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com