Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Custom Sort An AoA

by wind (Priest)
on Apr 01, 2014 at 20:28 UTC ( [id://1080638]=note: print w/replies, xml ) Need Help??


in reply to Custom Sort An AoA

A solution using List::MoreUtils.

Isn't the most efficient since it doesn't shortcut the comparing of array elements. Also duplicates the use of localized $a and $b, but overall I think this is very readable.

use List::MoreUtils qw(lastval pairwise); my @array = ( ['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa'], ); @array = sort {@$a <=> @$b or lastval {$_} pairwise {$a cmp $b} @$a, @ +$b} @array; use Data::Dump; dd \@array;
- Miller

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found