JupiterHost.Net wrote:
>> On Oct 3, JupiterHost.Net said:
>> 
>>> I have a list of strings that start with an uppercase B, Q, or Z
>>> 
>>> I need to sort them so they are in order of Q, B , then Z
>>> 
>>> Any ideas or input on how to efficiently do that with sort() or even
>>> map() is most appreciated :) perldoc -f sort|-f map didn't appear to
>>> address this situation :(
> 
> Jeff 'japhy' Pinyan wrote:
> 
>> I would use map() before and after sort() to "correct" leading
>> characters. 
>> 
>>   my @sorted =
>>     map { tr/123/QBZ/; $_ }
>>     sort
>>     map { tr/QBZ/123/; $_ }
>>     @data;
> 
> Xavier Noria wrote:
> 
>  > They all go in ASCII relative order except B <-> Q, thus a way to
>  get > it is to handle that special case and delegate to cmp the rest:
>  >
>  >     my @sorted = sort {
>  >         my $x = substr($a, 0, 1) . substr($b, 0, 1);
>  >         $x eq "BQ" || $x eq "QB" ? $b cmp $a : $a cmp $b;
>  >     } @array;
>  >
>  > I used the concatenation for clarity, you see the idea anyway if 
>  that's > too expensive for your usage.
> 
> Brilliant! I'll benchmark those ideas :) Thanks you two!

Read this for an explanation of Jeff's solution:

http://en.wikipedia.org/wiki/Schwartzian_transform

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to