You could use inet_aton() from Socket to transform the IP into a sortable string.

use strict; use warnings; use 5.014; use Socket; my %hosts = ( host0 => { ip => q{17.198.26.3} }, host1 => { ip => q{113.34.87.9} }, host2 => { ip => q{23.6.8.136} }, host3 => { ip => q{10.45.18.207} }, host4 => { ip => q{192.168.31.8} }, host5 => { ip => q{10.17.227.3} }, host6 => { ip => q{192.168.1.6} }, host7 => { ip => q{172.16.1.1} }, host8 => { ip => q{3.77.45.22} }, host9 => { ip => q{17.216.1.1} }, ); say qq{$_ - $hosts{ $_ }->{ ip }} for map { $_->[ 0 ] } sort { $a->[ 1 ] cmp $b->[ 1 ] } map { [ $_, inet_aton($hosts{ $_ }->{ ip } ) ] } keys %hosts;

The output.

host8 - 3.77.45.22 host5 - 10.17.227.3 host3 - 10.45.18.207 host0 - 17.198.26.3 host9 - 17.216.1.1 host2 - 23.6.8.136 host1 - 113.34.87.9 host7 - 172.16.1.1 host6 - 192.168.1.6 host4 - 192.168.31.8

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Sorting Hash Value Object's IPv4 Address by johngg
in thread Sorting Hash Value Object's IPv4 Address by razmeth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.