Here's a solution using the so-called Schwartzian transform for sorting the keys (the leading "C" is stripped off, and the the key list is sorted by the number before the dash, and then by the number after the dash:
#!/usr/bin/perl use warnings; use strict; my %keys; $keys{'C10'}->{'C10-1'}=1; $keys{'C10'}->{'C10-2'}=1; $keys{'C10'}->{'C10-20'}=1; $keys{'C10'}->{'C10-3'}=1; $keys{'C200'}->{'C200-1'}=1; $keys{'C200'}->{'C200-20'}=1; $keys{'C200'}->{'C200-3'}=1; $keys{'C200'}->{'C200-21'}=1; $keys{'C30'}->{'C30-1'}=1; $keys{'C30'}->{'C30-2'}=1; $keys{'C30'}->{'C30-30'}=1; $keys{'C30'}->{'C30-4'}=1; sub sorted { return map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } map { [ $_, split("-", substr($_,1)) ] } @_; } foreach my $key (sorted(keys %keys)) { print "'$key' => ", $keys{$key}, "\n"; foreach my $subkey (sorted(keys %{$keys{$key}})) { print " '$subkey' => $keys{$key}->{$subkey}\n"; } }

Arjen


In reply to Re: How to sort hash tables alpha-numeric by Aragorn
in thread How to sort hash tables alpha-numeric by juo

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.