Three questions in that post as far as I can see, so here are three answers...

Q1 - Does

@FullSort = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] || $a->[2] <=> $b->[2]} @data;

do the same thing as

@data = sort { $a->[0] cmp $b->[0] } @data; @data = sort { $a->[1] cmp $b->[1] } @data; @data = sort { $a->[2] <=> $b->[2] } @data;

Answer - no! The first code applies three sorting criteria at the same time. Only if two elements match on one criteria will the next one be applied. The second code applies three sorting criteria in turn. The second sort will completely overwrite the results of the first sort.

Q2 - Is the following interpretation correct (talking about sort subroutines)?

return -1; # moves the item lower return +1; # goes above the previous one return 0; # equals

I know what you're saying and it does pretty much sum up how sort subs work, but a better way to explain it is that the sort subroutine is passed two values in $a and $b. Your sub can then do whatever it likes to compare these two values, but must return -1, 0 or 1 depending on whether $a sorts before, in the same place as or after $b.

Q3 - sorting line speeds. I'd do something like this:

my %speeds = (115kbps => 10, 2Mbps => 20, 10Mbps => 30, T1 LINE => 40); my @sorted = sort { $speeds{$a} <=> $speeds{$b} } @unsorted;
--
<http://www.dave.org.uk>

European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/>

In reply to Re: Are these sorting methods the same? by davorg
in thread Are these sorting methods the same? by Russ

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.