Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Custom Sort An AoA

by AnomalousMonk (Monsignor)
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"], ]


Comment on Re: Custom Sort An AoA
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-27 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls