You can control sorting by putting a sort routine after the sort keyword, e.g.:
for my $m (sort nameFirst ('fred', 'alf', 'tom', 'bert', 'name', 'pete +')) { print "$m\n"; } sub nameFirst { if ($a eq 'name') { -1; } elsif ($b eq 'name') { 1; } else { 0; } }
sort calls your routine to test a pair which arrive in your routine as $a and $b. You can return the values explicitly although it is customary to just auto-evaluate for sort routines. Returning -1 or 1 tells sort what relative position to use for a given pair. 0 means 'evaluate equal or don't care, so leave as is'. The above produces:
name fred alf tom bert pete
update: if you have more complex rules than this, you can drill down to an evaluation subroutine or hash. For example, suppose the order should be name first and aref last
sub nameBlahAref { naeval($a) <=> naeval($b); } sub naeval { $_[0] eq 'name' and return 1; $_[0] eq 'aref' and return 3; return 2; }
The <=> operator compares what is either side of it numerically and returns the required -1, 0 or 1 accordingly.

One world, one people


In reply to Re: Ordering meta tags with HTML::Element by anonymized user 468275
in thread Ordering meta tags with HTML::Element by HeadScratcher

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.