Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Custom Sort An AoA

by AnomalousMonk (Canon)
on Apr 01, 2014 at 17:40 UTC ( #1080608=note: print w/replies, xml ) Need Help??

in reply to Custom Sort An AoA

A decorated solution. Assumes none of the strings in the sub-arrays contain nulls, hence you may not consider this approach 'general'! Should be default-sort fast.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @list = ( ['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa'], ); ;; my @sorted = map undecorate(\@list, $_), sort map decorate(\@list, $_), 0 .. $#list ; dd \@sorted; ;; sub decorate { my ($ar_list, $i) = @_; ;; my $elems = @{ $ar_list->[$i] }; my @smeti = reverse @{ $ar_list->[$i] }; ;; return pack qq{N (Z*)$elems N}, $elems, @smeti, $i } ;; sub undecorate { my ($ar_list, $decoration) = @_; ;; return $ar_list->[ unpack 'x* X[N] N', $decoration ]; } " [ ["one"], ["two"], ["qqq", "xyz", "aaa"], ["mmm", "def", "ghi"], ["zzz", "def", "ghi"], ["blah", "asdf", "foo", "bar"], ]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1080608]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2016-09-26 19:18 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (489 votes). Check out past polls.