themask
asked on
Sorting on multiple fields
I have the O'Reily Programming Perl book (2nd Ed.) that has sample code for sorting on multiple fields. The example is on page 218. I'll include the example here too.
sub prospects {
$money{$b} <=> $money{$a}
or
$height{$b} <=> $height{$a}
or
$age{$a} <=> $age{$b}
or
$lastname{$a} <=> $lastname{$b}
or
$a cmp $b;
}
@sortedclass = sort prospects @class;
I understand what this code is supposed to do but I can't figure out the format of the data. Can someone explain what @class looks like. Is it some kind of array of hash tables?
I need to sort a set of records based on multiple fields and print out the list of records in order. Any help with this would be appreciated.
sub prospects {
$money{$b} <=> $money{$a}
or
$height{$b} <=> $height{$a}
or
$age{$a} <=> $age{$b}
or
$lastname{$a} <=> $lastname{$b}
or
$a cmp $b;
}
@sortedclass = sort prospects @class;
I understand what this code is supposed to do but I can't figure out the format of the data. Can someone explain what @class looks like. Is it some kind of array of hash tables?
I need to sort a set of records based on multiple fields and print out the list of records in order. Any help with this would be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@money{@class}=(22,22,22,1
@height{@class}=(150,150,1
@age{@class}=(9,10,9,10,9,
@lastname{@class}=(1,2,3,1