in reply to criteria based array sorting

Update: fixed typo's

Well to complete this thread, I started digging around in the old nodes, and came across Language::MySort by japhy at Language::MySort.

#!/usr/bin/perl use Language::MySort; my @order = ('s'..'z','a'..'r','R'..'Z','A'..'Q'); print q(order-> 's'..'z','a'..'r','R'..'Z','A'..'Q'),"\n"; *my_sort = lang_sort(@order); my @needs_sorting = qw(Psdgs ldfgs Esdds aSdg Sgsdfs zdef Xrty Msdb es +df sgfsdfg Osgr T); print join ',',@needs_sorting,"\n"; my @sorted = my_sort(@needs_sorting); print join ',',@sorted,"\n";
OUTPUT: order-> 'r'..'z','a'..'s','R'..'Z','A'..'Q' Psdgs,ldfgs,Esdds,aSdg,Sgsdfs,zdef,Xrty,Msdb,esdf,sgfsdfg,Osgr,T, sgfsdfg,zdef,aSdg,esdf,ldfgs,Sgsdfs,T,Xrty,Esdds,Msdb,Osgr,Psdgs,

Replies are listed 'Best First'.
Re: Re: criteria based array sorting
by Willard B. Trophy (Hermit) on Feb 13, 2004 at 18:23 UTC
    I'm a big fan of Sort::ArbBiLex, myself. It's not quite the perfect lexical sort module, but it'll do for 99.995% of the population.

    --
    bowling trophy thieves, die!

Re: Re: criteria based array sorting
by Limbic~Region (Chancellor) on Feb 13, 2004 at 21:13 UTC
    zentara,
    It appears you have a typo in that there is an overlap with 'r'..'z' and 'a'..'s' but it shouldn't cause a problem. Personally, I would steal a page from broquaint's bag of tricks:
    #!/usr/bin/perl use strict; use warnings; my @unsorted = ( qw (Psdf lPik Easd aKwe SSdf eqwer scfgh Oegb rqwer T +) ); my %order = map {$_ => /[r-z]/ ? 'A' : /[a-q]/ ? 'B' : /[R-Z]/ ? 'C' : + 'D'} ('a'..'z', 'A'..'Z'); my @sorted = map { substr($_ , 1) } sort map { $order{ substr($_,0,1) } . $_ } @unsorted; print "$_\n" for @sorted;
    Cheers - L~R