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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|