Russ has asked for the wisdom of the Perl Monks concerning the following question:
In response to this code:
an Anonymous Monk asked:@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;
Will progressive sorting functon, like below, function the same way as using the || ?:
And a little extra question -- can I make comparisions inside the subroutine, and does the following mean...# 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;
??? .. or I don't understand it at all ? :))return -1; # moves the item lower return +1; # goes above the previous one return 0; # equals
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 |