I just want to comment on the line
$a->[2] cmp $b->[2] || $a->[2] <=> $b->[2]
If this was meant to be the sort subroutine, it has some problems (other than the aready mention ones). The or (||) is operating on the results of two comparison operators that return -1, 0 , 1. This will not a result that sort will be able to use. This will be interpreted as "Compare column 2 as text and if the elements are equal compare them as numbers". so it is still wrong. cmp is use for string and <=> is used for numbers. By the time you are executing the sort, you should know what the type is.

Update: Based on this node : Q&A Node on multicolumn sorting and some other research I realized my orignal statment was not fully correct. The node does not mention that this works because the or operater (||) short circuits if not false. The whole expression returns the -1 or 1 value for the first sub expression that has non false value or return 0 if they are all equal. I guess a real monk should add this to the node.


In reply to Re: sorting and array by [2] by admiraln
in thread sorting and array by [2] by kevyt

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.