Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RFC: yet another sorting technique

by blazar (Canon)
on Jul 27, 2005 at 15:37 UTC ( #478647=perlmeditation: print w/ replies, xml ) Need Help??

(I am not fanatic about efficiency and I am aware of the usual caveats about premature optimization. But I think this makes an interesting subject for a meditation.)

I know of Guttman & Rosler's article about sort. In it they argue in favour of using sort's "internal sort" i.e. without an explicit sort sub.

The technique consists in packing both the key on which to sort on (lexicographically) and the original data into strings and to recover the original data later.

But this may not be always/easily applicable e.g. if the items to be sorted are complex data structures themselves. So I thought that one may still take advantage of the fast "internal" sort doing something like this:

my @sorted=do{ my $n; my %stuff=map { func($_) . ':' . $n++ => $_ } @unsrt; @stuff{sort keys %stuff}; };
or perhaps
my @sorted=do{ my @keys=map func($unsrt[$_]) . ":$_", 0 .. $#unsrt; @unsrt[ map +(split /:/)[-1], sort @keys ]; };

(the second form may even be cast into a single statement like thus:

my @sorted=@unsrt[ map +(split /:/)[-1], sort map func($unsrt[$_]) . ":$_", 0 .. $#unsrt ];
but that wouldn't probably make for much clarity.)
Update: it occurs to me now that
my @sorted=map $unsrt[ (split /:/)[-1] ], sort map func($unsrt[$_]) . ":$_", 0 .. $#unsrt;
is even simpler and not that unreadable. Probably it's the best of all the code examples given here... well as far as my taste is concened!

Whatever, I have never seen such techniques before and I'm curious to hear some comments about them. I have not done any benchmark yet and I'm also looking for some suggestions about possibly interesting target cases.

Comment on RFC: yet another sorting technique
Select or Download Code
Re: RFC: yet another sorting technique
by salva (Monsignor) on Jul 27, 2005 at 15:44 UTC
    that is more or less what Sort::Key does, though implemented in C.
Re: RFC: yet another sorting technique
by rnahi (Curate) on Jul 27, 2005 at 15:50 UTC

    Lately, our Tutorials seem to be more neglected than usual.

    About sorting techniques, including the one you are explaining here, check Resorting to Sorting.

Re: RFC: yet another sorting technique
by tye (Cardinal) on Jul 27, 2005 at 16:32 UTC
Re: RFC: yet another sorting technique
by creamygoodness (Curate) on Aug 06, 2005 at 21:43 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-22 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (177 votes), past polls