Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Efficiently sorting array by non-alpha strings

by BrowserUk (Patriarch)
on Feb 20, 2004 at 16:18 UTC ( [id://330584]=note: print w/replies, xml ) Need Help??


in reply to Efficiently sorting array by non-alpha strings

This would do it and is easier to extend than the multiple ifs or an if/else cascade.

#!/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 $order = "\0local\0state\0national\0"; sub by_locale { index( $order, "\0" . lc( $a->{ fld_type } ) . "\0" ) <=> index( $order, "\0" . lc( $b->{ fld_type } ) . "\0" ) } my @sorted_advocates = sort by_locale @advocates; warn Dumper("unsorted_advocates", \@advocates); warn Dumper("sorted_advocates", \@sorted_advocates);

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: Efficiently sorting array by non-alpha strings
by knowmad (Monk) on Feb 20, 2004 at 16:30 UTC

    I added your solution to my benchmark and it comes in side-by-side with my original sort solution. However, yours is ++ for elegance and extendability. Thanks for the input!

    -Wm

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found