Since your example was sketchy, I'll have to give you only an example:
my @aoh = (
{ first => 'fred', last => 'flintstone', age => 30 },
{ first => 'wilma', last => 'flintstone', age => 26 },
{ first => 'pebbles', last => 'flintstone', age => 3 },
{ first => 'barney', last => 'rubble', age => 28 },
{ first => 'betty', last => 'rubble', age => 24 },
{ first => 'bammbamm', last => 'rubble', age => 2 },
{ first => 'mr.', last => 'slate', age => 35 },
);
# The boss comes first!
my %lastname_sortorder = qw(
flintstone 2
rubble 3
slate 1
);
my @sorted = sort {
$lastname_sortorder{$a->{last}} <=> $lastname_sortorder{$b->{last}}
+or # primary is lastname sort order
$a->{age} <=> $b->{age} # secondary is age
} @aoh;
The key here is to create an "ordering table" which is used for the comparison, then look up your non-linear sort key in this ordering table, and sort on that instead.
-- Randal L. Schwartz, Perl hacker
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.