What happens when you do a default sort on them?
The second one comes first, because they're being lexically sorted and 0 is before s, or whatever.
So I sort them numerically.
And strict complains, because the arguments aren't numeric.
But the cool thing is that it gets it right anyway.
Here's an example:
use strict; my @x = qw( 1xxx 2xxx 300x 10xx ); @x = sort @x; print "default sort: @x\n"; @x = sort {$a <=> $b} @x; print "numeric sort: @x\n";
Which will hopefully do the same for you as happened for me. Strict will complain, but sort will happen just fine.
And then how about this, which seemed like something I should try:
@x = sort {$a =~/^d+/ <=> $b =~/^d+/} @x; print "regex sort: @x\n";
That actually came up with the 'right' answer -- numeric sorting, as well, plus no complaints from strict, but I have to admit I have no idea what I'm doing.
Do I have a question? I'm not sure I do. Unless it's "how do I sort, numerically, things which are not entirely numeric, without getting lots of error messages?".
--
Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.
M-J D
In reply to Sort Says "not numeric" then sorts numeric anyway? by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |