merlyn,
I am likely wrong, but I don't see mdog's code being a reinvention of Sort::Fields. The code is designed to arbitrarily sort the keys of a multi-dimensional hash based off user supplied key names. It also sorts ascending or descending based off user input and auto-magically detects numerical versus ASCIIBetical sorting.

What mdog has provided is not very flexible. It assumes a specific structure. I was going to try and make it more generic, but gave up after only duplicating the fuctionality. Why you might ask - besides getting bored, look at the atrocity:

#!/usr/bin/perl use strict; use Tie::Hash::Sorted; use Scalar::Util 'looks_like_number'; my @keys = qw(name number); tie my %hash, 'Tie::Hash::Sorted'; @{ $hash{$_} }{ @keys } = ($_, '7.7') for qw(Me Chuck Zed); @{ $hash{Wife} }{ @keys } = qw(Wife 7.6); @{ $hash{Dad} }{ @keys } = qw(Dad 53); @{ $hash{Michael} }{ @keys } = qw(Michael 24); sub Build_Sort { my $hash = shift; my (@col , @ord); $_ % 2 ? (push @ord , $_[$_]) : (push @col , $_[$_]) for 0 .. $#_; my $sort_it = sub { for ( 0 .. $#col ) { if ( $ord[$_] eq 'dsc' ) { if ( looks_like_number( $hash->{$a}{$col[$_]} ) && loo +ks_like_number( $hash->{$b}{$col[$_]} ) ) { next if ($hash->{$b}{$col[$_]} <=> $hash->{$a}{$co +l[$_]}) == 0; return $hash->{$b}{$col[$_]} <=> $hash->{$a}{$co +l[$_]}; } else { next if ($hash->{$b}{$col[$_]} cmp $hash->{$a}{$co +l[$_]}) == 0; return $hash->{$b}{$col[$_]} cmp $hash->{$a}{$co +l[$_]}; } } else { if ( looks_like_number( $hash->{$a}{$col[$_]} ) && loo +ks_like_number( $hash->{$b}{$col[$_]} ) ) { next if ($hash->{$a}{$col[$_]} <=> $hash->{$b}{$co +l[$_]}) == 0; return $hash->{$a}{$col[$_]} <=> $hash->{$b}{$co +l[$_]}; } else { next if ($hash->{$a}{$col[$_]} cmp $hash->{$b}{$co +l[$_]}) == 0; return $hash->{$a}{$col[$_]} cmp $hash->{$b}{$co +l[$_]}; } } } }; return sub { my $h = shift; [ sort $sort_it keys %$h ]; }; } tied( %hash )->Sort_Routine( Build_Sort(\%hash, number => 'dsc', name +=> 'asc') ); print "$hash{$_}{name}\t$hash{$_}{number}\n" for keys %hash;

Cheers - L~R


In reply to Re^2: Sort Multidimensional Hash By Multiple Columns by Limbic~Region
in thread Sort Multidimensional Hash By Multiple Columns by mdog

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.