G'day Limbic~Region,

This is a general solution which appears to tick all the boxes:

@list = sort { @$a <=> @$b || do { my $cmp; ($cmp = $a->[-$_] cmp $b->[-$_]) ? last : next for 1 .. @$a; $cmp; } } @list;

Here's my test:

Update: I wrote that this was "a general solution"; however, having reviewed some of your comments in this thread, I see what I've provided wasn't what you meant.

I've converted the code I originally posted, into what I suspect is closer to what you want; but, if it's not, please provide more details about what you're looking for.

#!/usr/bin/env perl use strict; use warnings; use Scalar::Util qw{looks_like_number}; my @master_alpha_list = ( ['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa'] ); my @master_num_list = ( [ 0, 1, 2, 3 ], [ 2 ], [ 9, 3, 6 ], [ 1 ], [ 5, 3, 6 ], [ 7, 8, 1 ], ); my @alpha_asc = sort { @$a <=> @$b || gen_sort() } @master_alpha_list; my @alpha_dsc = sort { @$a <=> @$b || gen_sort(1) } @master_alpha_list +; my @num_asc = sort { @$a <=> @$b || gen_sort() } @master_num_list; my @num_dsc = sort { @$a <=> @$b || gen_sort(1) } @master_num_list; use Data::Dump; dd $_ for (\@alpha_asc, \@alpha_dsc, \@num_asc, \@num_dsc); sub gen_sort { my ($desc) = @_; my $compare = looks_like_number($a->[0]) ? \&cmp_num : \&cmp_alpha +; my ($x, $y) = $desc ? ($b, $a) : ($a, $b); my $cmp; ($cmp = $compare->($x->[-$_], $y->[-$_])) ? last : next for 1 .. @ +$a; return $cmp; } sub cmp_alpha { $_[0] cmp $_[1] } sub cmp_num { $_[0] <=> $_[1] }

Output:

[ ["one"], ["two"], ["qqq", "xyz", "aaa"], ["mmm", "def", "ghi"], ["zzz", "def", "ghi"], ["blah", "asdf", "foo", "bar"], ] [ ["two"], ["one"], ["zzz", "def", "ghi"], ["mmm", "def", "ghi"], ["qqq", "xyz", "aaa"], ["blah", "asdf", "foo", "bar"], ] [[1], [2], [7, 8, 1], [5, 3, 6], [9, 3, 6], [0 .. 3]] [[2], [1], [9, 3, 6], [5, 3, 6], [7, 8, 1], [0 .. 3]]

-- Ken


In reply to Re: Custom Sort An AoA by kcott
in thread Custom Sort An AoA by Limbic~Region

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.