Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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 about the Monastery: (8)
As of 2016-06-28 06:06 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (350 votes). Check out past polls.