in reply to Re: Numeric Sort for Stringified Value (How to Avoid Warning)
in thread Numeric Sort for Stringified Value (How to Avoid Warning)

This breaks, e.g. on @old = ("10.5 AA", "100 NO");

10.5 < 100, but "10.5" gt "0100".

Replies are listed 'Best First'.
Re^2: Numeric Sort for Stringified Value (How to Avoid Warning)
by radiantmatrix (Parson) on Sep 20, 2005 at 17:53 UTC

    Excellent catch, and a great test case. My approach was wrong. In order to sort this in a DWIM-like way, this would probably work better:

    my @old = ("10.5 AA", "100 NO"); my @new = map { join(' ',@$_) } sort { ($b->[0] <=> $a->[0]) || ($b->[1] cmp $a->[1]) } map { ($1,$2) if m/(.*?)\s(.*)/ } @old;

    That's a bit cryptic. Basically, split each component of @old into number and string "columns", then use a two-criteria sort (sort on number, then string) and rejoin the "columns" into a string again.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law