Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Two sort in single Array set

by johngg (Canon)
on Aug 31, 2017 at 11:39 UTC ( [id://1198405]=note: print w/replies, xml ) Need Help??


in reply to Two sort in single Array set

Just to illustrate another way, though probably too complicated for this simple problem. You can use an array slice if you sort the array indices. I use array references since that's what the OP shows and use pack/unpack and a GRT to do the sort. Note that I uc the severity so that "critical" sorts before "OK".

my $raSortedEvents = [ @{ $raEvents }[ map { unpack q{x12N}, $_ } sort map { pack( q{A8}, uc $raEvents->[ $_ ]->{ severity } ) . ( $raEvents->[ $_ ]->{ severity } eq q{OK} ? pack( q{l>}, $raEvents->[ $_ ]->{ event_age } ) : ~ pack( q{l>}, $raEvents->[ $_ ]->{ event_age } ) ) . pack( q{N}, $_ ); } 0 .. $#{ $raEvents } ] ];

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Two sort in single Array set
by Anonymous Monk on Aug 31, 2017 at 16:50 UTC
    BTW, this only works if event_age is a non-negative integer. Also, ew.
      this only works if event_age is a non-negative integer

      I suppose they could be recording events that haven't happened yet and if they are then an additional flag for negativity fixes the problem.

      my $raSortedEvents = [ @{ $raEvents }[ map { unpack q{x13N}, $_ } sort map { pack( q{A8}, uc $raEvents->[ $_ ]->{ severity } ) . ( $raEvents->[ $_ ]->{ severity } eq q{OK} ? pack( q{cl>}, $raEvents->[ $_ ]->{ event_age } < 0 ? 0 : 1, $raEvents->[ $_ ]->{ event_age } ) : pack( q{c}, $raEvents->[ $_ ]->{ event_age } < 0 ? 1 : 0 ) . ~ pack( q{l>}, $raEvents->[ $_ ]->{ event_age } ) ) . pack( q{N}, $_ ); } 0 .. $#{ $raEvents } ] ];

      Even more ew'ness I suppose but I did say this was over-complicated for the OP's problem and was just to illustrate a technique. It would perhaps look less dense if broken down into two stages.

      my @sortOrder = map { unpack q{x13N}, $_ } sort map { pack( q{A8}, uc $raEvents->[ $_ ]->{ severity } ) . ( $raEvents->[ $_ ]->{ severity } eq q{OK} ? pack( q{cl>}, $raEvents->[ $_ ]->{ event_age } < 0 ? 0 : 1, $raEvents->[ $_ ]->{ event_age } ) : pack( q{c}, $raEvents->[ $_ ]->{ event_age } < 0 ? 1 : 0 ) . ~ pack( q{l>}, $raEvents->[ $_ ]->{ event_age } ) ) . pack( q{N}, $_ ); } 0 .. $#{ $raEvents }; my $raSortedEvents = [ @{ $raEvents }[ @sortOrder ] ];

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found