[ back to toc ]

CGI & Perl advanced sorting

Date: 2002/01/23 15:59

Q:
I am working on sorting a flat-file record that has been imported into my
CGI script using an array. I sort this listing using the sort command and
subroutine, but if two or more entires end up being the same, how can I
specify a secondary column to sort by?

@schedule = sort byvenue (@schedulebo);
sub byvenue {
@a = split(/\|/,$a);
@b = split(/\|/,$b);
$a[3] cmp $b[3];
}

That is the code I am presently using.
Is there any way I can easily do this? Or is it not possible to pick a
secondary column to sort by if two or more results end up being the same
in the 4th column I specified already?

~*NAME-DELETED* *NAME-DELETED*
A:
The result that your subroutine returns is the sorting used by the
function sort. Thus you can write:

$res = $a[3] cmp $b[3];
return $res if $res != 0;
$res = $a[2] cmp $b[2];
return $res if $res != 0;

and so on.

Regards,
Peter

[ back to toc ]