in reply to Are these sorting methods the same?
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;
|
|---|