Author image Salvador Fandiño García


Sort::Key::IPv4 - sort IP v4 addresses


  use Sort::Key::IPv4 qw(ipv4sort);

  my @data = qw(;
  my @sorted = ipv4sort @data;

  use Sort::Key::IPv4 qw(ipv4keysort);

  my @sorted = ipv4keysort { $_->ip_address } @hosts;


This module extends the Sort::Key family of modules to support sorting of IP v4 addresses.

IPv4 addresses have to match the regular expression /^(\d)+\.(\d)+\.(\d)+\.(\d)+$/.


The functions that can be imported from this module are:

ipv4sort @data

returns the IPv4 addresses in @data sorted.

ripv4sort @data

returns the IPv4 addresses in @data sorted in descending order.

ipv4keysort { CALC_KEY($_) } @data

returns the elements on @array sorted by the IPv4 addresses resulting from applying them CALC_KEY.

ripv4keysort { CALC_KEY($_) } @data

is similar to ipv4keysort but sorts the elements in descending order.

ipv4sort_inplace @data
ripv4sort_inplace @data
ipv4keysort_inplace { CALC_KEY($_) } @data
ripv4keysort_inplace { CALC_KEY($_) } @data

these functions are similar respectively to ipv4sort, ripv4sort, ipv4sortkey and ripv4sortkey, but they sort the array @data in place.

ipv4_to_uv $key

converts the IPv4 value to a 32 bits unsigned integer.


Sort::Key, Sort::Key::Maker


Copyright © 2007, 2009 by Salvador Fandiño, <>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.