I find problems with your code that have not been commented on. Your specifically the direction part is a red herring that responders seemed to chase.

The problems with your mysort are that it is single purpose and it only sorts the %This_package::hash and nothing else. It is bad that one must read mysort to know that. The function is also poorly named and is defined at the wrong level. Defined in a logical, or problem domain modelling, sense.

If you can, use the standard style of sorting. This is to use the $a and $b variables to indicate how you are sorting. If you want a reversed sort you use $b $a to show that. This is to define the sort at a lower and more generic level.

I would suggest you use something like:

sub num_or_str { no warnings 'numeric'; $_[0] <=> $_[1] || $_[0] cmp $_[1] } my @sorted = sort num_or_str( $people{$a}->{age}, $people{$b}->{age} ), keys %people;
Then you can reverse the order of the arguments to num_or_str to reverse the sort. I consider that idiomatic Perl as below.

If you really need a direction flag variable, you could keep it readable:

my @stuff = qw/ 9 -1 -3 hi 0 bye 5 10 /; my $descending = 1; my @ar; if ( $descending ) { @ar = sort { num_or_str($b, $a) } @stuff; } else { @ar = sort { num_or_str($a, $b) } @stuff; }
If you want to structure code more like your sample you should put the function around the sort. That is what I meant about creating the function at the wrong level.
my $sort_by = 'age'; my $descending = 1; my $database = \%people; # return an ordered list of copies of keys of a # hash structured as described somewhere # usage: sort_people( \%sort_me, $by_me, $descending ) # Ascending sort is the default. # If $by_me is not a key that exists then ... # Maybe an arref should be returned. sub sort_people { my ( $people, $by, $desc) = @_; no warnings qw/ numeric uninitialized/; if ( $desc) { return sort { your_conditions( $b, $a)} keys %$people; } else { return sort { your_conditions( $a, $b)} keys %$people; } }
Then this higher level code is easier to understand:
my @sorted_keys = sort_people( \%people, $sort_on, $descending);
Be well,
rir

In reply to Re: sort direction by rir
in thread sort direction by rsiedl

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.