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

Re: Efficiently sorting array by non-alpha strings

by dreadpiratepeter (Priest)
on Feb 20, 2004 at 16:09 UTC ( [id://330577]=note: print w/replies, xml ) Need Help??


in reply to Efficiently sorting array by non-alpha strings

This ought to do it. WARNING: barely tested.
#!/usr/bin/perl use strict; use Data::Dumper; my @advocates = ( { fld_title => 'Rec1', fld_type => 'National', }, { fld_title => 'Rec2', fld_type => 'State', }, { fld_title => 'Rec3', fld_type => 'Local', }, ); my %map = ( local => 1, state => 2, national => 3 ); my @sorted_advocates = map {$_->[1]} sort {$a->[0] <=> $b->[0]} map {[$map{lc($_->{fld_type})},$_]} @advocates; warn Dumper("unsorted_advocates", \@advocates); warn Dumper("sorted_advocates", \@sorted_advocates);


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found