in reply to Sort problem
use strict; use Data::Dumper; my @in = ( [qw(M N)], [qw(M N P O)], [qw(C B A)], [qw(U S)], [qw(V T)], [qw(C G F E D)], [qw(C G F H)], [qw(C G F H I)], [qw(C G F H J)], [qw(M K)] ); sub arrayCompare { my @a = @$a; my @b = @$b; while ( 1 ) { return 0 if @a ==0 && @b == 0; return -1 if @a == 0; return 1 if @b == 0; my $cmp = $a[0] cmp $b[0]; return $cmp if $cmp != 0; shift @a; shift @b; } } my @out = sort arrayCompare @in; print Dumper @out;
(Side comment: I'm surprised at how many people missed the significance of "once the elements can be strings that could contain embedded spaces" and tried approaches based on concatenation.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sort problem
by BrowserUk (Patriarch) on Feb 26, 2003 at 20:46 UTC | |
by tachyon (Chancellor) on Feb 26, 2003 at 20:55 UTC | |
by BrowserUk (Patriarch) on Feb 26, 2003 at 21:09 UTC | |
by runrig (Abbot) on Feb 26, 2003 at 21:10 UTC | |
by hv (Prior) on Feb 27, 2003 at 00:17 UTC | |
by BrowserUk (Patriarch) on Feb 27, 2003 at 08:07 UTC | |
|
Re: Re: Sort problem
by tachyon (Chancellor) on Feb 26, 2003 at 19:47 UTC | |
by dws (Chancellor) on Feb 26, 2003 at 19:55 UTC | |
by tachyon (Chancellor) on Feb 26, 2003 at 20:10 UTC | |
by jsprat (Curate) on Feb 26, 2003 at 20:26 UTC | |
by tachyon (Chancellor) on Feb 26, 2003 at 20:48 UTC | |
by dws (Chancellor) on Feb 26, 2003 at 20:35 UTC | |
by tachyon (Chancellor) on Feb 26, 2003 at 20:41 UTC |