http://www.perlmonks.org?node_id=598572


in reply to RFC:A brief tutorial on Perl's native sorting facilities.

I think another good node about sorting is Better sorting than the ST (was: Perl is dying), I think this "trick" should be included in the tutorial as well.

{ my @keys = map {...} @list; my @idx = sort {$keys[$a] cmp $keys[$b]} (0..$#list); @list = @list[@idx]; } # or { my @keys = map {...} @list; @list = @list[ sort {$keys[$a] cmp $keys[$b]} (0..$#list) ]; }
As it doesn't create lots of tiny arrays it should even be quicker than ST. And it doesn't have the restrictions of GRT.