Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: sorting an array with decimal points

by syphilis (Archbishop)
on Jan 15, 2018 at 02:50 UTC ( [id://1207254]=note: print w/replies, xml ) Need Help??


in reply to Re^2: sorting an array with decimal points
in thread sorting an array with decimal points

Hi Laurent_R,

it might actually work thanks to the coercion of version numbers into decimal numbers imposed by the <=> operator

Yes, the spaceship operator will always compare in numeric context - and I was quite confident that if the only problem with levW's
sort { substr($a, 6,2) <=> substr($b, 6,2)} @array
was that it failed to sort in the correct order, then
sort { substr($a, 6) <=> substr($b, 6)} @array
would fix that.

But levW seemed to think that there was a problem re the decimal point (which I don't see), and I was a little unsure about other aspects of the requirements, so I shied away from investing much time into my reponse.

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: sorting an array with decimal points
by Laurent_R (Canon) on Jan 15, 2018 at 23:16 UTC
    Yes, the reason I answered your post was that I thought other monks might make the same mistake I initially made: that's why I thought it was worth showing that your solution worked for the sample data shown because of numeric coercion, and also to show the test.

    This being said, and as pointed out by AnomalousMonk in Re: sorting an array with decimal points, if the OP's data is really representing version numbers (and I think that's what they are) and not decimals, then your solution would fail on, for example, 5.2, 5.14, 5.16, 5.20, because it would sort 5.2 after 5.14 and 5.16 (which would be wrong for version numbers).

    But, of course, my assumptions that they are version numbers may also be wrong. Only levW can tell us what they really are.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found