Hi, sorting on the first field of the arrays instead of the 5th one, because you did not specify what to do with undef values (i.e. how to sort them):
use strict; use warnings; use Data::Dumper my %hash = ( '10857' => [ '10857', 'Test-host1', '192.168.162.124', undef, undef, undef, undef, 'Timeout while connecting to "192.168.162.124:1 +61"' ], '10715' => [ '10715', 'Test-Host2', '192.168.162.146', 'checkPing[{HOST.IP}]', '108', '108', '2014-04-01 20:24:01', '' ], '10778' => [ '10778', 'Test-Host3', '192.168.162.144', 'checkPing[{HOST.IP}]', '392', '359', '2014-04-01 20:21:12', '' ], '10582' => [ '10582', 'Test-Host3', '192.168.162.95', 'icmppingsec', '117.390000', '116.493000', '2014-04-01 20:23:03', '' ] ); my @sorted_array = sort {$a->[0] <=> $b->[0] } values %hash; print Dumper \@sorted_array;
You'll have to change the
{$a->[0] <=> $b->[0] }
part to something like: { (do something for undef values;) $a->4 <=> $b->4 }

The result:

ARRAY(0x404bfc18) 0 10582 1 'Test-Host3' 2 '192.168.162.95' 3 'icmppingsec' 4 117.390000 5 116.493000 6 '2014-04-01 20:23:03' 7 '' 1 ARRAY(0x40135fe0) 0 10715 1 'Test-Host2' 2 '192.168.162.146' 3 'checkPing[{HOST.IP}]' 4 108 5 108 6 '2014-04-01 20:24:01' 7 '' 2 ARRAY(0x40573600) 0 10778 1 'Test-Host3' 2 '192.168.162.144' 3 'checkPing[{HOST.IP}]' 4 392 5 359 6 '2014-04-01 20:21:12' 7 '' 3 ARRAY(0x4012a0d8) 0 10857 1 'Test-host1' 2 '192.168.162.124' 3 undef 4 undef 5 undef 6 undef 7 'Timeout while connecting to "192.168.162.124:161"'

In reply to Re: Need to sort and data structure based on values inside arrayrefs. by Laurent_R
in thread Need to sort and data structure based on values inside arrayrefs. by shekarkcb

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.