in reply to Re: Sorting Numbers & Text
in thread Sorting Numbers & Text

I get the same result with:
#!/usr/bin/env perl use strict; use warnings; use Data::Printer; my @unsorted = qw( 041351920234 Rabbit 0343120 041271024500 0430870 Ap +ple 041460301399); my @sorted = sort { $a <=> $b || $a cmp $b } @unsorted; p @unsorted; p @sorted;

Is your way more efficient memory-wise? Also, both of our approaches are giving me warnings like: Argument "Rabbit" isn't numeric in numeric comparison (<=>) at....

Replies are listed 'Best First'.
Re^3: Sorting Numbers & Text
by johngg (Canon) on Jul 12, 2012 at 22:10 UTC

    It seems that the warnings are due to a precedence problem that can be fixed with parentheses,

    knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > my @values = qw{ > 041351920234 > Rabbit > 0343120 > 041271024500 > 0430870 > Apple > 041460301399 > }; > > say for > map { $_->[ 0 ] } > sort { > ( $a->[ 1 ] <=> $b->[ 1 ] ) > || > ( > $a->[ 1 ] > ? $a->[ 0 ] <=> $b->[ 0 ] > : $a->[ 0 ] cmp $b->[ 0 ] > ) > } > map { [ $_, m{^\d} ? 1 : 0 ] } > @values;' Apple Rabbit 0343120 0430870 041271024500 041351920234 041460301399 knoppix@Microknoppix:~$

    Thanks for pointing it out.

    Cheers,

    JohnGG