I've on several occasions read about the Schwartzian Transform and I've tried it in small apps with simple sorts, with limited success. The advantage in it, I understand is being able to do more complex sorting as well as improved performance. But to apply ST to more complicated sorts has escaped me. I use a sub I wrote some time ago that I can pass an array ref(containing delimited records), a delimiter, the field I wish to sort on and a flag toggling whether or not I want unique output. In merlyn's tutorial he explains the inefficiency of doing splits inside your sort routines(this make great sense), I rely on splits heavily in this sub. I have included my sub for reference and wonder if I might get some suggestions about improving it’s performance using ST and also adding the ability to sort on more than one field.

Here’s my code:

sub sort_array_records { # sort_array_records takes a reference to an array such as \@myarr +ay as # the 1st arg followed by a delimeter, a key (numeric index) and a # boolean uniq flag if a unique array is expected in return. # sort_array_records returns the array in list context. If the key + has # an "n" appended to it the sort is done numerically. An example o +f # sort_array_records is: # @uniq_users = sort_array_records (\@passswd, ":", 0, 1); my ($array_2_sort_ref, $delim, $key, $uniq) = @_; my $numeric = 0; my @sorted_array = (); if ($key =~ /\d*n/) { $numeric = 1; $key =~ s/(^\d*)n/$1/; } if ($numeric) { @sorted_array = sort { (split /$delim/, $a)[$key] <=> (split / +$delim/, $b)[$key] } @$array_2_sort_ref; } else { @sorted_array = sort { (split /$delim/, $a)[$key] cmp (split / +$delim/, $b)[$key] } @$array_2_sort_ref; } if ($uniq) { my $prev = lc($sorted_array[$key]); my @uniq_elems = grep( lc((split /$delim/, $_)[$key]) ne $prev + && ($prev = lc((split /$delim/, $_)[$key]), 1), @sorted_array); return @uniq_elems; } else { return @sorted_array; } }

Incidentally, I did not write this sub from scratch but more expanded on ideas I found in the Perl Cookbook ISBN:ISBN 1565922433 and here at Perlmonks.

TIA

Sweetblood


In reply to Improving sorts using ST by sweetblood

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.