Russ has asked for the wisdom of the Perl Monks concerning the following question:

Moved from Q&A to allow a better discussion...

In response to this code:

@data = ( [qw(Bubba brutalski 20)], [qw(Bubba brutalski 10)], [qw(JoeBob brutalski 20)], [qw(Jethro brutalski 20)], [qw(Junior brutalski 20)], ); # Sorted by Name (Last, First) @ByName = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]} @data; # Sorted by Name, then score @FullSort = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] || $a->[2] <=> $b->[2]} @data;
an Anonymous Monk asked:

Will progressive sorting functon, like below, function the same way as using the || ?:

# Full sort by Name, surname, then score @data = sort { $a->[0] cmp $b->[0] } @data; @data = sort { $a->[1] cmp $b->[1] } @data; @data = sort { $a->[2] <=> $b->[2] } @data;
And a little extra question -- can I make comparisions inside the subroutine, and does the following mean...
return -1; # moves the item lower return +1; # goes above the previous one return 0; # equals
??? .. or I don't understand it at all ? :))

I need to sort an array with net line speeds... the line speeds can be like "10Mbps" "115kbps" "2Mbps" "T1 LINE" ... so in the ASCIIbetical sorting the 115kbps would go to the top, while the T1 monster would fall to the bottom...

So now I need some trick to make some "priority hash" or array where I'd put the proper order of sort...

thanx again!

Replies are listed 'Best First'.
Re: Are these sorting methods the same?
by davorg (Chancellor) on Jul 05, 2000 at 13:10 UTC

    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/>