in reply to Ordering meta tags with HTML::Element
sort calls your routine to test a pair which arrive in your routine as $a and $b. You can return the values explicitly although it is customary to just auto-evaluate for sort routines. Returning -1 or 1 tells sort what relative position to use for a given pair. 0 means 'evaluate equal or don't care, so leave as is'. The above produces:for my $m (sort nameFirst ('fred', 'alf', 'tom', 'bert', 'name', 'pete +')) { print "$m\n"; } sub nameFirst { if ($a eq 'name') { -1; } elsif ($b eq 'name') { 1; } else { 0; } }
update: if you have more complex rules than this, you can drill down to an evaluation subroutine or hash. For example, suppose the order should be name first and aref lastname fred alf tom bert pete
The <=> operator compares what is either side of it numerically and returns the required -1, 0 or 1 accordingly.sub nameBlahAref { naeval($a) <=> naeval($b); } sub naeval { $_[0] eq 'name' and return 1; $_[0] eq 'aref' and return 3; return 2; }
One world, one people
|
|---|