snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:
[ { '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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Two sort in single Array set
by choroba (Cardinal) on Aug 30, 2017 at 09:58 UTC | |
Re: Two sort in single Array set
by johngg (Canon) on Aug 31, 2017 at 11:39 UTC | |
by Anonymous Monk on Aug 31, 2017 at 16:50 UTC | |
by johngg (Canon) on Aug 31, 2017 at 22:46 UTC | |
Re: Two sort in single Array set
by Anonymous Monk on Aug 30, 2017 at 16:25 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |