Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by benizi (Hermit)
on Sep 19, 2005 at 16:42 UTC ( [id://493193]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://493193]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found