in reply to Sorting Numbers & Text
Do the sort using a Schwartzian Transform by storing an indicator on whether the value is alpha or numeric as well as the value itself. The outer sort is alpha then numeric and the inner sort comparator is chosen in a ternary depending on the indicator.
knoppix@Microknoppix:~$ perl -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:~$
I hope this is helpful.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting Numbers & Text
by frozenwithjoy (Priest) on Jul 12, 2012 at 21:36 UTC | |
by johngg (Canon) on Jul 12, 2012 at 22:10 UTC | |
|
Re^2: Sorting Numbers & Text
by PriNet (Monk) on Jul 12, 2012 at 21:40 UTC | |
by johngg (Canon) on Jul 12, 2012 at 22:25 UTC | |
by johngg (Canon) on Jul 13, 2012 at 18:41 UTC |