Suppose I have the following list of things to sort:
@things_to_sort = (
{ author => 'bart', title => 'skateboarding' },
{ author => 'lisa', title => 'postmodernism' },
{ author => 'marge', title => 'hairstyles' },
{ author => 'lisa', title => 'THIS BOOK FIRST' },
{ author => 'homer', title => 'donuts' },
{ author => 'bart', title => 'coolness' }
);
My sort requirements are:
If the title is 'THIS BOOK FIRST', that needs to go first, followed by any other books by the same author alphabetically by title. All other books follow alphabetically by author, then title.
Is it possible to write a sort routine so that
@sorted_things = sort by_twisted_requirements @things_to_sort
will do what I need?
Or do I need to break it up into
@things_at_the_top and
@the_rest, sort those and then smack the list back together?
I think I can do that, but it's hella ugly. Can anyone suggest an elegant solution to this problem?
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, details, 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, summary, 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.