Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Two sort in single Array set

by sundialsvc4 (Abbot)
on Aug 30, 2017 at 22:22 UTC ( #1198364=note: print w/replies, xml ) Need Help??


in reply to Two sort in single Array set

If it were me, I would probably err extremely on the side of clarity, e.g.

sub sort_comp { my $result = ($a->{severity} <=> $b->{severity}; if ($result == 0) { # severity is the same -- dig deeper if ($a->{severity} eq 'critical'} { $result = ($a->{event_age} <=> $b->{event_age}); #asc } else { $result = ($b->{event_age} <=> $a->{event_age}); #desc } } return $result; }

(Hope I wrote that right ...)

In other words I would strive to make it ridiculously clear what the sort-rule was.   (I would use an explicit rather than an in-line sub to make it even more clear.)   And I would definitely put the logic into a sort-subroutine instead of following the Anonymous suggestion.   This is a run-of-the-mill sort on two keys, not “two sorts” as suggested.

Replies are listed 'Best First'.
Re^2: Two sort in single Array set
by kcott (Chancellor) on Aug 31, 2017 at 05:48 UTC
    "(Hope I wrote that right ...)"

    Instead of hoping, why not actually check your work. This took me just a few seconds:

    $ perl -MO=Deparse -e ' sub sort_comp { my $result = ($a->{severity} <=> $b->{severity}; if ($result == 0) { # severity is the same -- dig deeper if ($a->{severity} eq 'critical'} { $result = ($a->{event_age} <=> $b->{event_age}); #asc } else { $result = ($b->{event_age} <=> $a->{event_age}); #desc } } return $result; } ' syntax error at -e line 3, near "};" syntax error at -e line 5, near "critical}" syntax error at -e line 7, near "} else" Unmatched right curly bracket at -e line 12, at end of line -e had compilation errors.

    After you've fixed your code, please ensure you follow the "How do I change/delete my post?" guidelines when correcting your what you wrote.

    — Ken

Re^2: Two sort in single Array set
by eyepopslikeamosquito (Chancellor) on Aug 31, 2017 at 22:39 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1198364]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2017-12-14 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (399 votes). Check out past polls.

    Notices?