Use this . I think you wud do better later to move the sort function to 
a seperate sub

foreach $key (sort {
     ($$h{$a}{taste} eq $$h{$b}{taste} )  ? $$h{$a}{name} cmp 
$$h{$b}{name}: $$h{$a}{taste} \cmp $$h{$b}{taste}
     }  (keys %$h)) {
     print "$key \n";
}



Jeff Aa wrote:
> Folks,
> 
> I want to sort my masked hashes into neat little piles for easier
> digestion:
> Please note this is _example_ data 8-)
> 
> my $h = {
>   a => { name => 'apple', taste => 3 },
>   b => { name => 'peach', taste => 2 },
>   c => { name => 'banana', taste => 2 },
> }
> 
> I want to sort first on taste and then on name, [and in the real world
> on other things as well..] ATM I do this
> 
> foreach my $item ( sort 
>   {
>     return $a->{taste} <=> $b->{taste} unless $a->{taste}==$b->{taste};
>     return $a->{name} cmp $b->{name};
>   } values %$h ) {
>   print "Name: $item->{name} taste: $item->"taste}\n";
> }
> 
> How would a Perl god code a multi-key sort function?
> What about when there are LOTs of fields?
> 
> e.g.
> 
> sort 
>   {
>     foreach my $field (qw( taste weight cost size )) {
>       return $a->{$field} <=> $b->{$field} 
>         unless $a->{$field}==$b->{$field};
>     }
>     foreach my $field (qw( name colour supplier store )) {
>       return $a->{$field} cmp $b->{$field} 
>         unless $a->{$field} eq $b->{$field};
>     }  } values %$h
> 
> An acolyte begs for divine inspiration! TIA
> 
> Jeff
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to