http://www.perlmonks.org?node_id=1080624


in reply to Re^3: Custom Sort An AoA
in thread Custom Sort An AoA

I'd do it too with an extra helper function, but I would keep the first condition out (for readability, flexibility and reuse)

something like

sub cmp_vec { # return either -1,0 or 1 comparing over two equally sized arrays } sort { @$a <=> @$b or cmp_vec($a,$b) } @list;

for full flexibility a cmp-function could be passed as call-back in third position.

As a side note, I'm not sure if this could be done with Perl6 Meta ops, something like @a >>cmp<< @b (after reversing of course).

Cheers Rolf

( addicted to the Perl Programming Language)

update

something like

sub cmp_vec { my ($a,$b) = @_; for (reverse 0.. @$a-1) { if (my $x= $a->[$_] cmp $b->[$_]) { return $x } } return 0; ]