N
Nick Wedd
Here is my program:
use strict;
sub by_incsub1 { $$a[1] <=> $$b[1]; }
sub by_incsub2 { $$a[2] <=> $$b[2]; }
my $i;
my @a = ( ['a',3,1],['b',2,2],['c',1,1],['d',1,2],['e',2,1],['f',3,2] );
my @result = sort by_incsub1 @a;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
print "\n";
@result = sort by_incsub2 @result;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
print "\n";
@result = sort by_incsub1 @result;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
and here is its output:
c11 d12 b22 e21 a31 f32
c11 e21 a31 d12 b22 f32
c11 d12 e21 b22 a31 f32
This is exactly what I hoped for. In fact it is better (more useful to
me) than I feel I have any right to expect. When it sorts on one
criterion, it leaves the items that tie under that criterion in the
order they were in before.
Now this is exactly what I want it to do. But no documentation that I
can recall promises that it will do that. Output like
c11 d12 e21 b22 a31 f32
a31 c11 e21 b22 d12 f32
d12 c11 e21 b22 f32 a31
would still meet the specification of "sort".
Can I rely on Perl's sort to continue to do what I want, or is it
implementation-dependent?
Nick
use strict;
sub by_incsub1 { $$a[1] <=> $$b[1]; }
sub by_incsub2 { $$a[2] <=> $$b[2]; }
my $i;
my @a = ( ['a',3,1],['b',2,2],['c',1,1],['d',1,2],['e',2,1],['f',3,2] );
my @result = sort by_incsub1 @a;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
print "\n";
@result = sort by_incsub2 @result;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
print "\n";
@result = sort by_incsub1 @result;
foreach $i ( 0..5 )
{ print "$result[$i][0]$result[$i][1]$result[$i][2] "; }
and here is its output:
c11 d12 b22 e21 a31 f32
c11 e21 a31 d12 b22 f32
c11 d12 e21 b22 a31 f32
This is exactly what I hoped for. In fact it is better (more useful to
me) than I feel I have any right to expect. When it sorts on one
criterion, it leaves the items that tie under that criterion in the
order they were in before.
Now this is exactly what I want it to do. But no documentation that I
can recall promises that it will do that. Output like
c11 d12 e21 b22 a31 f32
a31 c11 e21 b22 d12 f32
d12 c11 e21 b22 f32 a31
would still meet the specification of "sort".
Can I rely on Perl's sort to continue to do what I want, or is it
implementation-dependent?
Nick