Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: sorting an array with decimal points

by salva (Abbot)
on Jan 15, 2018 at 08:04 UTC ( #1207264=note: print w/replies, xml ) Need Help??

in reply to Re: sorting an array with decimal points (updated)
in thread sorting an array with decimal points

Better yet, just use the Sort::Naturally module from CPAN

Sort::Naturally does not handle numbers with decimals correctly. For instance, nsort '1.10', '1.31', '1.4' returns 1.4 1.10 1.31.

But you can use Sort::Key::Natural instead which provides a set of functions for sorting strings embedding numbers with decimals:

use Sort::Key::Natural qw(natwfsort); my @sorted = natwfsort @array;

Update: Though, taking into account that the data in the OP says Patch_, maybe those numbers are actually version numbers and then the part after the decimal dot may be better sorted as an independent integer, which is what a regular natural sort does...

Replies are listed 'Best First'.
Re^3: sorting an array with decimal points
by Laurent_R (Canon) on Jan 15, 2018 at 20:12 UTC
    Your comment is relevant and right, but I would tend to agree with your update and I assumed that these numbers are version numbers rather than decimals, so that 1.4 1.10 1.31 would probably be the correct order for such data.

    But we will be able to figure out for sure only if the OP comes back and tells us.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2019-10-13 22:16 GMT
Find Nodes?
    Voting Booth?