__END__
Note that the comparison can be expensive. To reduce the number of
comparisons, you can use "The Schwartzian Transform" (see
<URL:
http://www.stonehenge.com/merlyn/UnixReview/col06.html>).
#!/usr/bin/perl
use strict;
use warnings;
my $separator = ";";
my @data_list = qw(
5;3;7
5;3;7;9
5;3;7;9;
5;3;7;9;10
1;5;11
11;4;8
1;3;9
1;3;9
1;39;11
);
my @data_sort = map { $_->[1] }
sort vector_compare
map { [ [ split /;/ ], $_ ] }
@data_list;
{
local $, = "\n";
print @data_sort;
}
sub vector_compare {
my @a = @{ $a->[0] };
my @b = @{ $b->[0] };
while ( @a or @b ) {
my $ai = shift @a;
my $bi = shift @b;
return 1 if defined $ai and not defined $bi;
return -1 if not defined $ai and defined $bi;
return $ai <=> $bi unless $ai == $bi;
}
return 0; # same number of elements, all equal
}
__END__
Let's take a look at that
my @data_sort = map { $_->[1] }
sort vector_compare
map { [ [ split /;/ ], $_ ] }
@data_list;
Here, for each element of @data_list
map { [ [ split /;/ ], $_ ] } @data_list;
creates a reference to an anonymous array with two elements. The first
element is a reference to another anonymous array containing the split
numbers. This is used by the comparison routine. The second element is
the original string from @data_list. This is used for reconstituting the
list after sorting is done.
sort vector_compare
sorts this list according to the vector_compare sub above
map { $_->[1] }
then reconstitutes the original array.
Sinan
--
A. Sinan Unur <
[email protected]>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html