in reply to Re: Two sort in single Array set
in thread Two sort in single Array set

BTW, this only works if event_age is a non-negative integer. Also, ew.

Replies are listed 'Best First'.
Re^3: Two sort in single Array set
by johngg (Canon) on Aug 31, 2017 at 22:46 UTC
    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