Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: sort after first char of hash key

by wind (Priest)
on Jun 02, 2011 at 14:48 UTC ( [id://907810]=note: print w/replies, xml ) Need Help??


in reply to sort after first char of hash key

Just use substr to extract the proper values to sort by

my @list = map {"E$_"} (1..20); for my $k (sort {substr($a, 1) <=> substr($b, 1)} @list) { print "$k "; }

Note: If your keys don't actually all start with E, but you instead want to sort first by the alpha section and then numerically by the integer suffix, then you simply must extract the sections before sorting like so:

my @list = ("A15", "Y2", map {"E$_"} (1..20)); for my $k ( map {$_->[0]} sort {$a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} map {[$_, /(\D+)(\d+)/]} @list ) { print "$k "; }

Replies are listed 'Best First'.
Re^2: sort after first char of hash key
by danj35 (Sexton) on Jun 02, 2011 at 16:37 UTC

    Excellent suggestion and one I should have thought of before. It is near the end of the week...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://907810]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found