# RE: Sorting

```On May 10, Nic LAWRENCE said:

>> > Can anybody suggest the most efficient method to do the following...
>> >
>> > I have an array of email aliases like the following:
>> > [EMAIL PROTECTED]:                       sys
>> > [EMAIL PROTECTED]:                       coookiecom
>> > [EMAIL PROTECTED]:               niccicamcom
>> > [EMAIL PROTECTED]:      katyland-news_site14-request
>> > [EMAIL PROTECTED]:             majordomo_site4
>> > [EMAIL PROTECTED]:              melanier
>> > [EMAIL PROTECTED]:            louisefolds
>> > [EMAIL PROTECTED]:               swccom
>> >
>>
>>  @new = sort {
>>               my(\$A) = \$a =~ /\@([^:]+)/;
>>               my(\$B) = \$b =~ /\@([^:]+)/;
>>               return  \$A cmp \$B;
>>       } @ary;
>
>Oooo... I like that... more or less the same as Peter suggested but a little
>more self contained ... gonna have to remember that kinda structure.
>
>thnx i'll try it out :)

That sorting method does a lot of work -- that is, it does things more
than once.  I suggest you use a schwarztian transform, or the Orcish
manuever, to increase speed.

@new =
map  { \$_->[0] }
sort { \$a->[1] cmp \$b->[1] }
map  { [ \$_, /\@([^:]+)/ ] }
@orig;

or

{
my %cache;
@new = sort {
(\$cache{\$a}) = \$a =~ /\@([^:]+)/ if not exists \$cache{\$a};
(\$cache{\$b}) = \$b =~ /\@([^:]+)/ if not exists \$cache{\$b};
\$cache{\$a} cmp \$cache{\$b}
} @orig;

You can read about these two at:

http://www.pobox.com/~japhy/docs/sorting.html

--
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734

```