I promised to post the code for which I was seeking assistance once I had the project completed. Here it is, taken from CPAN module Data::Presenter, version 1.02:
sub sort_by_column { my $self = shift; my $columns_selected_ref = shift; my %data = %{$self}; _validate_args($columns_selected_ref, \%fp); $columns_selected_ref = _verify_presence_of_index(\%data, $columns_selected_ref); my @records; foreach my $k (keys %data) { push (@records, $data{$k}) unless ($reserved{$k}); } my $sortref = _sort_maker( map { _make_single_comparator( $_ ) } @{$columns_selected_ref} ); return _extract_columns_selected( [ sort $sortref @records ], $columns_selected_ref, ); } sub _verify_presence_of_index { my $dataref = shift; my $columns_selected_ref = shift; my @fields = @{$dataref->{fields}}; my $index = ${$dataref}{index}; my @columns_selected = @{$columns_selected_ref}; my %cols = map {$_, 1} @columns_selected; unless ($cols{$fields[$index]}) { # line 205 carp "Field '$fields[$index]' which serves as unique index for + records must be one of the columns selected for output; adding it to + end of list of columns selected: $!"; push @columns_selected, $fields[$index]; } return [ @columns_selected ]; } sub _sort_maker { my @littlesubs = @_; sub { foreach my $sub (@littlesubs) { my $result = $sub->(); return $result if $result; } }; } sub _make_single_comparator { my $field = shift; my $sort_order = $fp{$field}->[1]; my $sort_type = $fp{$field}->[2]; my $idx = $fieldlabels{$field}; no warnings qw(uninitialized numeric); my %subs = ( U => { a => sub { lc($a->[$idx]) cmp lc($b->[$idx]) }, n => sub { $a->[$idx] <=> $b->[$idx] }, s => sub { $a->[$idx] cmp $b->[$idx] }, }, D => { a => sub { lc($b->[$idx]) cmp lc($a->[$idx]) }, n => sub { $b->[$idx] <=> $a->[$idx] }, s => sub { $b->[$idx] cmp $a->[$idx] }, }, ); $subs{$sort_order}{$sort_type}; } sub _extract_columns_selected { my ($intermed_ref, $columns_selected_ref) = @_; my @results; foreach my $record (@{$intermed_ref}) { my @temp; foreach my $col (@{$columns_selected_ref}) { push @temp, $record->[$fieldlabels{$col}]; } push @results, [ @temp ]; } return [ @results ]; }

Thanks again, tanktalus and brian.

Jim Keenan


In reply to Re^6: Building a sorting subroutine on the fly by jkeenan1
in thread Building a sorting subroutine on the fly by jkeenan1

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.