Sorting IP Addresses

Discussion in 'Ruby' started by barjunk, Sep 25, 2007.

  1. barjunk

    barjunk Guest

    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.
    barjunk, Sep 25, 2007
    #1
    1. Advertising

  2. > #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]}"
    }
    Daniel Sheppard, Sep 25, 2007
    #2
    1. Advertising

  3. barjunk

    Phrogz Guest

    On Sep 24, 6:31 pm, barjunk <> 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]}"
    }
    Phrogz, Sep 25, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mar Thomas

    Sorting IP addresses

    Mar Thomas, Sep 5, 2003, in forum: Java
    Replies:
    5
    Views:
    5,143
    Marco Schmidt
    Sep 6, 2003
  2. Peter Murray

    Sorting email addresses by domain

    Peter Murray, Nov 11, 2004, in forum: Python
    Replies:
    5
    Views:
    430
    Mike Meyer
    Nov 11, 2004
  3. Jp Calderone

    Re: Sorting email addresses by domain

    Jp Calderone, Nov 11, 2004, in forum: Python
    Replies:
    2
    Views:
    360
    Alex Martelli
    Nov 14, 2004
  4. Peter Murray

    Sorting email addresses by domain

    Peter Murray, Nov 11, 2004, in forum: Python
    Replies:
    0
    Views:
    282
    Peter Murray
    Nov 11, 2004
  5. namespace1
    Replies:
    3
    Views:
    879
Loading...

Share This Page