<?xml version="1.0" encoding="windows-1252"?>
<node id="999789" title="Multiple Sort on selected column" created="2012-10-18 13:56:35" updated="2012-10-18 13:56:35">
<type id="115">
perlquestion</type>
<author id="994332">
huchister</author>
<data>
<field name="doctext">
Greeting, I am new to PERL, and I dont know how to implement this multiple sort key. 

I am trying to sort given array with given selected column. 

See below code

&lt;code&gt;
#!/usr/bin/perl

use strict;


my @employees = ( 
    { FIRST =&gt; 'Bill',   LAST =&gt; 'Gates',     
      SALARY =&gt; 600000, AGE =&gt; 45 },
    { FIRST =&gt; 'George', LAST =&gt; 'Tester',
      SALARY =&gt;  55000, AGE =&gt; 29 },
    { FIRST =&gt; 'Sally',  LAST =&gt; 'Developer', 
      SALARY =&gt;  55000, AGE =&gt; 29 },
    { FIRST =&gt; 'Joe',    LAST =&gt; 'Tester',    
      SALARY =&gt;  55000, AGE =&gt; 29 },
    { FIRST =&gt; 'Steve',  LAST =&gt; 'Ballmer',   
      SALARY =&gt; 600000, AGE =&gt; 41 } 
);

sub seniority {
    $a-&gt;{FIRST} cmp $b-&gt;{FIRST} or $a-&gt;{AGE} &lt;=&gt; $b-&gt;{AGE}
}

my $sort = "FIRST, AGE";
my @sorts = split( ',', $sort );
my @ranked;

    for( @sorts ) 
    { 
	@ranked = sort seniority @employees;
    }
foreach my $emp (@ranked) {
    print "$emp-&gt;{SALARY}\t$emp-&gt;{AGE}\t$emp-&gt;{FIRST}\t$emp-&gt;{LAST}\n";
}

&lt;/code&gt;

Let say I want to sort only the Last, and Salary. then I'd have to change {FIRST} or {AGE} value in this case. 

Also, What if I want to add $sort key, ex) "FIRST, AGE, LAST", I'd have to add method to seniority, but dont know how. 

This would be great example for multiple sort given key variables. I would try to use basic 'sort' method as possible, not modifying or inserting new Sort::Key, or any other. 

UPDATE : This problem was solved using kennethk's method. Please see below for additional info. </field>
</data>
</node>
