It looks to me as if you are not doing a sort, but rather grouping only. The order is the order returned by the keys statement, which is undefined (disclaimer: it is snowing here; I had to battle Jack Frost twice, and the snowplow once this morning, so I may be as hazy as the sky outside of my window).

To sort, you could nested loops on sort keys... (as noted by SuicideJunkie above) at each level of the data structure. This might be the simplest option.

You could also extract all levels of the structure's keys ([[reg1,ctry1,city1][reg2,ctry2,city2]...]), sort these, and then extract the data from the hash given these sorted values.

You could also join the keys into a single string for each entry (["reg1:ctry1:city1","reg2:ctry2:city2"...]), sort these, and extract the data from the hash using these sorted values (be careful of what you choose to use as the delimiter here).

On the last two, a Schwarzian Transform might (or might not) buy you some performance or understanding for your maintenance programmers, but you would need to benchmark the options to be sure what works best in your situation.

This all depends on your data structures (see kennethk above), how you build them, and how large your data set is.

Update: Added some clarification and credit. I was way too slow posting my response ;-)

--MidLifeXis


In reply to Re: Sort a Hash 3 Levels Deep by MidLifeXis
in thread Sort a Hash 3 Levels Deep by Dru

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.