Hi, I have records store in array :
[ { 'appname' => 'AAA', 'severity' => 'critical', 'line' => 1, 'event_age' => 12 }, { 'appname' => 'BBB', 'severity' => 'OK', 'line' => 2, 'event_age' => 32 }, { 'appname' => 'CCC', 'severity' => 'critical', 'line' => 3, 'event_age' => 4 }, { 'appname' => 'DDD', 'severity' => 'critical', 'line' => 4, 'event_age' => 22 }, { 'appname' => 'EEE', 'severity' => 'OK', 'line' => 5, 'event_age' => 4 }, { 'appname' => 'FFF', 'severity' => 'critical', 'line' => 6, 'event_age' => 1, }, { 'appname' => 'GGG', 'severity' => 'critical', 'line' => 7, 'event_age' => 8 }, { 'appname' => 'HHH', 'severity' => 'OK', 'line' => 8, 'event_age' => 10 }, { 'appname' => 'III', 'severity' => 'critical', 'line' => 9, 'event_age' => 7 }, { 'appname' => 'JJJ', 'severity' => 'critical', 'line' => 10, 'event_age' => 15 }, { 'appname' => 'KKK', 'severity' => 'OK', 'line' => 11, 'event_age' => 26 }, { 'appname' => 'LLL', 'severity' => 'critical', 'line' => 12 'event_age' => 23 }]

I want to sort above records based on the event_age :

such that first it should show all " severity = critical " records in descending order then ; remaining records with " severity = ok " in ascending order

Eg: if there are only 12 records and 4 are critical and 8 are OK .Then its should sort 4 records based on event_age in descending order and 8 records based on event_age in ascending order Below code in am using ::

#To sort the records for (@records){ if ($_->{'severity'} eq "critical"){ #sort the array @records and assign line number @records = sort{ $a->{event_age} <=> $b->{event_age} } @records; for my $i (0 .. $#records) { $records[$i]->{line} = $i + 1; } #End# } } # To Restrict the row count to 12 to fit the screen my $max = 12; for (@records){ if ($_->{'line'} < $max){ #DoNothing } } #Set Visibility to "No" if records are more then 12 for (@records+1..$max){ push @records, { line => $_, severity => "", ticketnum => "", appname => "", appname => "", event_age => "" , visible => "No", appspectname => "" }; } print Dumper \@records;

Please suggest with the correct approach/logic.


In reply to Two sort in single Array set by snehit.ar

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.