Link to home
Start Free TrialLog in
Avatar of themask
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.
Avatar of ozo
ozo
Flag of United States of America image

@class=qw(aa bb cc dd ee ff);
@money{@class}=(22,22,22,11,11,11);
@height{@class}=(150,150,160,160,150,160);
@age{@class}=(9,10,9,10,9,10);
@lastname{@class}=(1,2,3,1,2,3);  #are you sure this is numeric?
ASKER CERTIFIED SOLUTION
Avatar of samay
samay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial