John Almberg wrote at Wed, 25 Sep 2002 22:17:57 +0200: > I'm trying to sort a SQL table that contains a character-type field that > contains mostly numbers. This field always contains either a number or a > number followed by a character. Like '57' or '57a'. > > I'd like to sort the table *numerically* on this field, not *alphabetically* > on this field. That is, I'd like the table to be sorted like: > > 1 ... > 2a ... > 3 ... > 4d ... > > NOT like: > > 1 ... > 11 ... > 111a ... > 2a ... > 22 ... > > See what I mean? This is a common problem, I think, when you sort an > character type field that contains numbers. The sort comes out all wrong.
If your strings all start with the number you're interested, the problem is so common that it is Perl's standard behaviour. no warnings 'numeric'; my @sorted_table = sort {$a <=> $b} @table; The difference to the standard sort is that you specify with {$a <=> $b} a numeric sort and strings are interpreted in numeric context with their leading numbers. That's also a moment, where it is right to switch off Perl's warning for numeric contexts of strings with non-digit-character, as you really want to use Perl's standard conversion routine. Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]