Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
From what I gather you want everything numerical only up front, followed by anything with a letter in it sorted asciibetically. Here is some code using the ever popular Schwartzian Transform:
use strict; use warnings; my %keys; @keys{('a'..'n')}= ('2','1','12','a12','a9', 'a2b3','a2b4','b2b4','1a2','a2bb5', 'a2bb5a','a2bb5b',654,9); my @foo = map { $_->[0] } sort newsort map { [$_, $keys{$_} =~ /^(\d+)$/ ? ($keys{$_},1) : $keys{$_} ] } keys %keys; print "KEY:$_ VALUE:$keys{$_}\n" foreach (@foo); sub newsort { if ( defined $a->[2] and defined $b->[2] ){ if ($a->[1] > $b->[1]) {return 1} return -1; } elsif ( defined $b->[2] or defined $a->[2]) { if ( defined $a->[2] ) { return -1 } return 1; } elsif ( $a->[1] gt $b->[1] ) { return 1; } elsif ( $b->[1] gt $a->[1] ) { return -1; } return 0; } #newsort __DATA__ KEY:b VALUE:1 KEY:a VALUE:2 KEY:n VALUE:9 KEY:c VALUE:12 KEY:m VALUE:654 KEY:i VALUE:1a2 KEY:d VALUE:a12 KEY:f VALUE:a2b3 KEY:g VALUE:a2b4 KEY:j VALUE:a2bb5 KEY:k VALUE:a2bb5a KEY:l VALUE:a2bb5b KEY:e VALUE:a9 KEY:h VALUE:b2b4
-enlil

In reply to Re: hash sort by value - Alpha Numeric by Enlil
in thread hash sort by value - 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found