I have an array of hashes, @Tree. One of the elements in each hash, Path is itself an array of digits. For each element in @Tree, $_->{Path} can have from one to five (at present - could be even more) elements. And I need to sort @Tree so that it's sorted first by $_->{Path}->[0], then by $_->{Path}->[1] and so on.

In case that doesn't make any sense, I've done
for my $h (@Tree) { print "$_ " for @{$h->{Path}}; print "\n"; }
... for before and after, to show what I'm trying to achieve:


BEFORE
5 32 37 5 32 38 5 32 39 3 12 11 19 20 3 12 11 21 3 12 22 3 12 23 5 32 40 3 12 24 5 32 41 3 25 5 32 42 2 26 5 32 43 2 26 27 2 26 28 2 26 29 0 1 1 2 1 3 3 12 10 1 4 3 12 11 1 5 3 12 2 6 3 12 13 3 12 7 3 12 11 19 14 3 12 8 2 26 30 3 12 11 19 15 3 12 9 5 32 31 3 12 11 19 16 5 32 3 12 11 19 17 5 32 33 3 12 11 19 18 5 32 34 3 12 11 19 5 32 35 5 32 36
AFTER:
0 1 2 1 3 1 4 1 5 2 6 2 26 2 26 27 2 26 28 2 26 29 2 26 30 3 12 3 12 7 3 12 8 3 12 9 3 12 10 3 12 11 3 12 11 19 3 12 11 19 14 3 12 11 19 15 3 12 11 19 16 3 12 11 19 17 3 12 11 19 18 3 12 11 19 20 3 12 11 21 3 12 13 3 12 22 3 12 23 3 12 24 3 25 5 32 5 32 31 5 32 33 5 32 34 5 32 35 5 32 36 5 32 37 5 32 38 5 32 39 5 32 40 5 32 41 5 32 42 5 32 43
I've tried lots of different variations on
for (5..0) { @Tree = sort { $a->{Path}->[$_] <=> $b->{Path}->[$_] } @Tree; }
... none of which worked. In the end I gave up in disgust and did
for my $page (@Tree) { my $Sort = 0; my $M = 100; for (@{$page->{Path}}) { $Sort += $M * $_; $M = $M / 100; } $page->{Sort} = $Sort; } @Tree = sort {$a->{Sort} <=> $b->{Sort}} @Tree;
... which works, but is UGLY and probably slow and fattening. I'd really love to see a more elegant solution, if some monk can suggest one. Now, I got this result with the following rather clunky method:

§ George Sherston

2002-07-06 Edit by Corion: Added readmore tag


In reply to Complex Sort - using varying numbers of parameters by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.