rsiedl has asked for the wisdom of the Perl Monks concerning the following question:
This returns#!/usr/bin/perl use strict; use warnings; my %bar = ( # Name => [Age, Pets, Profession] 'Grant'=>[29,4,'Doctor'], 'Roberts'=>[32,1,'Priest'], 'Johnson'=>[29,3,'MD'], 'Harper'=>[23,2,'Student'] ); # sort by age and print foreach my $name ( sort { $bar{$b}[0]<=>$bar{$a}[0] } keys %bar ) { my ($age,$pets,$prof) = @{$bar{$name}}; print "Name: $name\nAge: $age\nPets: $pets\nProf: $prof\n"; } # end-foreach exit;
Name: Roberts Age: 32 Pets: 1 Prof: Priest Name: Johnson Age: 29 Pets: 3 Prof: MD Name: Grant Age: 29 Pets: 4 Prof: Doctor Name: Harper Age: 23 Pets: 2 Prof: StudentBut I would like to sort by age first and then by number of pets second. The above code doesnt always return in the correct order for the pets part.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Double sort in hash values
by ikegami (Patriarch) on Nov 22, 2004 at 15:13 UTC | |
by davido (Cardinal) on Nov 22, 2004 at 16:39 UTC | |
by rsiedl (Friar) on Nov 22, 2004 at 15:17 UTC |