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

Re: Custom Sort An AoA

by AnomalousMonk (Abbot)
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 scrutinizing the Monastery: (16)
As of 2015-07-01 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls