Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I was building sort keys such that a simple strcmp()/memcmp() could be used as the comparison function in a complex sort even before I found Perl (which was just before Perl 4 was released).

It turns out that this technique is very handy in Perl because it usually makes for the fastest possible sort by avoiding specifying a comparison function:

my @sorted= map { RESTORE($_) } sort map { XFORM($_) } @list;

The main problem with the technique is that you need to build the key (XFORM) in such a way that the original record can be reconstructed from it (RESTORE).

That often makes this technique difficult to use, making it more of a specialized technique, not something you'd feel you could use all the time.

I'd previously used techniques that avoid this RESTORE() step but they still weren't general enough or elegant enough to be the one way to sort for me.

I finally came up with a technique that I suspect will be "the one way to sort" for me (below).

It also has the feature that it produces a "stable" sort, that is, records with identical keys are kept in their original order relative to each other.

sub XFORM { # Extract the sort key from $_[0] and return it. # This will often be written in-line # rather than as a real subroutine. } my @sorted= @list[ map { unpack "N", substr($_,-4) } sort map { XFORM($list[$_]) . pack "N", $_ } 0..$#list ];

If you want to sort parallel lists, then you'd keep the sorted list of indices around:

my @index= map { unpack "N", substr($_,-4) } sort map { XFORM($name[$_]) . pack "N", $_ } 0..$#name; @name= @name[@index]; @data= @data[@index];

In reply to fast, flexible, stable sort by tye

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found