Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: New Alphabet Sort Order

by furry_marmot (Pilgrim)
on Apr 04, 2011 at 22:55 UTC ( #897404=note: print w/ replies, xml ) Need Help??


in reply to Re^2: New Alphabet Sort Order
in thread New Alphabet Sort Order

Polyglot,

I think BrowserUK's solution is the direction you'll end up going. The ST and GRT he referred to are the Schwartzian Transform and Guttman Rosler Transform, respectively. You only need the first.

The link explains it, but basically you transform a list of things you want to sort into a list of two-element arrays. The first element is the key, set up so you can easily sort it. The second is the original element, untouched. You can do it in separate steps, but the transform is more efficient if you have a lot of elements. The key thing you need is a function that can create a function that will create a key that you can sort on.

Here's an example, sorting movie names, done separately first.
use Lingua::EN::Numbers qw(num2en); sub make_key { $_ = shift; s/^(?:The|An|A) // || s/^[^A-Z_]+(\d+)/num2en; return $_; } my @movies = ( '(500) Days of Summer', # F for Five hundred 'The Music Man', # M for Music 'The Good, the Bad, and the Ugly' # G for Good ); my @tmp = (); for (@movies) { push @tmp [ make_key($_), $_ ] # 2-element anonymous array } @tmp = sort { $a->[0] cmp $b->[0] } @tmp; # sort on first elements @movies = map { $_->[1] } @tmp; # pull off second element from ea +ch # anonymous array print "$_\n" for @movies; __END__ Prints: (500) Days of Summer The Good, the Bad, and the Ugly The Music Man
Now, MUCH less complicated:
use Lingua::EN::Numbers qw(num2en); sub make_key { $_ = shift; s/^(?:The|An|A) // || s/^[^A-Z_]+(\d+)/num2en; return $_; } my @movies = ( '(500) Days of Summer', # F for Five hundred 'The Music Man', # M for Music 'The Good, the Bad, and the Ugly' # G for Good ); # Here's the transform. Read from the bottom up. my @movies = # original elements replaced with same +, but sorted map { $_->[1] } # pull off second element from each sort { $a->[0] cmp $b->[0] } # sort arrays on first elements map { [ make_key($_), $_ ] } # 2-element anonymous array becomes ne +w $_ @movies; print "$_\n" for @movies; __END__ Prints: (500) Days of Summer The Good, the Bad, and the Ugly The Music Man
Good luck!

--marmot

UPDATE: Corrected a typo bug in the ST that came from copying the first version.


Comment on Re^3: New Alphabet Sort Order
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-07-29 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls