Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Sorting IP Addresses

Reply
Thread Tools

Sorting IP Addresses

 
 
barjunk
Guest
Posts: n/a
 
      09-25-2007
I'm having to sort some IP addresses that are stored in a hash. I
came up with the code below to accomplish the task. I have two
questions:

- What if any is a better way to have done this?
- Given that the hash has to be the mac address, could I store the
hash values in a different way to make this easier?


require 'ipaddr'

hash={}
hash["00:19:d2:7b:55:10"]=["c","12.12.19.11",3]
hash["00:18:39:0d:a1:e9"]=["b","12.12.19.83",1]
hash["00:11:50:18:0c:0e"]=["a","12.12.19.81",2]

#the second index is the value item to sort by.
#this particular sort would fail if [1][1] was not an IP address

sortedarray = hash.sort { |a,b|
IPAddr.new(a[1][1]).to_i <=> IPAddr.new(b[1][1]).to_i
}

sortedarray.each { |item|

print "mac:", item[0] + "\n"
print "IP:", item[1][1] + "\n"
}


Mike B.

 
Reply With Quote
 
 
 
 
Daniel Sheppard
Guest
Posts: n/a
 
      09-25-2007
> #the second index is the value item to sort by.
> #this particular sort would fail if [1][1] was not an IP address
>=20
> sortedarray =3D hash.sort { |a,b|
> IPAddr.new(a[1][1]).to_i <=3D> IPAddr.new(b[1][1]).to_i
> }
>=20
> sortedarray.each { |item|
>=20
> print "mac:", item[0] + "\n"
> print "IP:", item[1][1] + "\n"
> }


sortedarray =3D hash.sort_by {|mac, settings|
IPAddr.new(settings[1])
}
sortedarray.each { |mac, settings|
puts "mac: #{mac}"
puts "IP: #{settings[1]}"
}

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      09-25-2007
On Sep 24, 6:31 pm, barjunk <(E-Mail Removed)> wrote:
> I'm having to sort some IP addresses that are stored in a hash. I
> came up with the code below to accomplish the task. I have two
> questions:
>
> - What if any is a better way to have done this?
> - Given that the hash has to be the mac address, could I store the
> hash values in a different way to make this easier?
>
> require 'ipaddr'
>
> hash={}
> hash["00:19:d2:7b:55:10"]=["c","12.12.19.11",3]
> hash["00:18:39:0d:a1:e9"]=["b","12.12.19.83",1]
> hash["00:11:50:18:0c:0e"]=["a","12.12.19.81",2]
>
> #the second index is the value item to sort by.
> #this particular sort would fail if [1][1] was not an IP address
>
> sortedarray = hash.sort { |a,b|
> IPAddr.new(a[1][1]).to_i <=> IPAddr.new(b[1][1]).to_i
>
> }
>
> sortedarray.each { |item|
>
> print "mac:", item[0] + "\n"
> print "IP:", item[1][1] + "\n"
>
> }


# May not be faster, but doesn't rely on the ipaddr library
hash={}
hash["00:19:d2:7b:55:10"]=["c","1.12.19.11",3]
hash["00:18:39:0d:a1:e9"]=["b","12.12.19.83",1]
hash["00:11:50:18:0c:0e"]=["a","12.12.19.81",2]
hash["00:00:00:08:c3:1e"]=["a","2.12.19.81",2]
hash["fe:00:00:08:c6:2f"]=["a","204.12.19.81",2]

sorted_array = hash.sort_by{ |mac, settings|
# Break the IP into an array of integers
# sort_by will sort by the first entry first, then the second, and
so on
settings[1].split('.').map{ |digits| digits.to_i }
}

# Note that you can break apart the array of arrays in the iteration
# And use puts and string interpolation for simpler printing
sorted_array.each { |mac, settings|
puts "mac: #{mac}"
puts "IP: #{settings[1]}"
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses cagdas.gerede@gmail.com C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
Re: Sorting email addresses by domain Jp Calderone Python 2 11-14-2004 01:47 PM
Sorting email addresses by domain Peter Murray Python 5 11-11-2004 08:36 PM
Sorting IP addresses Mar Thomas Java 5 09-06-2003 04:19 AM



Advertisments