in reply to Data Structure advice

I think this is a completely general way to do this. It will need some cleanup to be used in a real app though, because of the globals. (i was struggling with getting bizarre_sort() to take parameters properly. :(
#!/usr/bin/perl -w use strict; my @order_array; my %types = ( 0 => 'alpha', 1 => 'alpha', 2 => 'numeric', ); { my @data = ( ["sean", "peters", 29], ["matt", "horner", 23], ["dorthy", "radley", 49], ["tom", "curtis", 52], ["sean", "jones", 38], ); print "By first name:\n"; sorter([0], @data); print "\nBy last name:\n"; sorter([1], @data); print "\nby age:\n"; sorter([2], @data); print "\nby first then last:\n"; sorter([0,1], @data); print "\nby first then age:\n"; sorter([0,2], @data); } sub sorter { my ($order, @data) = @_; @order_array = @$order; foreach my $person (sort bizarre_sort @data ) { for my $i (0 .. 2) { print $person->[$i] . " "; } print "\n"; } } sub bizarre_sort { foreach my $field_num (@order_array) { my $result = low_checker($field_num, $types{$field_num}, $a, $ +b); return $result if $result; } # in case there was equality on all keys return 1; } sub low_checker { my ($field_num, $type, $a, $b) = @_; if ( $type eq "alpha" ) { if ( $a->[$field_num] cmp $b->[$field_num] ) { return ($a->[$field_num] cmp $b->[$field_num]); } } elsif ( $type eq "numeric" ) { if ( $a->[$field_num] <=> $b->[$field_num] ) { return ($a->[$field_num] <=> $b->[$field_num]); } } else { confess(); } }
longwinded, someone can probably make this cleaner.