Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( [id://3333]=superdoc: 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.

In reply to RFC: yet another sorting technique by blazar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-06-19 10:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.