I don't think this has anything that isn't in one or other of the other posts, but avoiding two levels of dereference four times inside the sort block, should more than compensate for the simplicity of using reverse, to avoid having two of them.

I don't have much of a problem with your generic sort block. If the fields are consistantly numeric or non-numeric, it will do the right thing, and if they are not, it probably means that someone entered bad data and so long as the bad stuff is sorted to one end or the other it does much matter which.

The one time it could give you a surprise if your DOB contained stuff like "12th Jan 1901" and "12th december 2004", (ie. non-numeric strings that start with numbers), which would be grouped together by day, but otherwise be in a random ordering. Then again, sorting dates is pain unless they are in a fully numeric format (eg.YYYYMMDD) or similar. One thing that did strike me as odd was you maintaining both a DOB and AGE fields? If so, you create a consistancy problem with non-normailsed data.

#! perl -slw use strict; use Data::Dumper; sub rndStr{ join'', @_[ map{ rand $#_ } 1 .. shift ] } my %hash = map { $_ => { name => rndStr( 10, 'a'..'z' ), age => int 18+rand 80, sex => ( 'M', 'F', 'U' )[ rand 3 ], } } 1000001 .. 1000010; for my $orderBy ( qw[ name age sex ] ) { my $dir = ( qw[ ASC DESC ] )[ rand 2 ]; ## Just one, inline sort block with dereferencing only done once. my @sorted = sort{ local $^W; $a <=> $b || $a cmp $b } map{ $_->{$orderBy} } values %hash; ## Reverse if need be @sorted = reverse @sorted if $dir eq 'DESC'; ## And display print "\n$orderBy $dir"; print for @sorted } __END__ P:\test>498186 name ASC bbfxkykvpk bmhymyxlbm fcqxfmqtpc gbwvxqdecg glqaexowct ixptsmdruw kijumgktwv mpnnmcncjt porblnobmp uiwodoxvqo age DESC 90 76 76 58 48 47 45 35 26 22 sex DESC U U U U U U U M F F

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re: sort direction by BrowserUk
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.