Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: sorting an array with decimal points

by AnomalousMonk (Chancellor)
on Jan 15, 2018 at 17:16 UTC ( #1207295=note: print w/replies, xml ) Need Help??


in reply to sorting an array with decimal points

The implicit specification of the OP seems unclear in one respect: How should a version number of, e.g., '4.20' compare to '4.2'? Is '4.20' equal to or greater than '4.2'? Is a version number like '4.20' even possible in levW's application? (Update: And are these even version numbers?)

Except for kcott's solution (update: and Laurent_R's solution), all the solutions presented in this thread seem to use a numeric sort comparison, which will sort '4.2' adjacent to '4.20'.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: sorting an array with decimal points
by Laurent_R (Canon) on Jan 15, 2018 at 19:52 UTC
    ... all the solutions presented in this thread seem to use a numeric sort comparison, which will sort '4.2' adjacent to '4.20'.
    Not the Schwartzian Transform solution I originally presented (Re: sorting an array with decimal points). Even though the OP is indeed unclear and speaks about decimal point numbers, I considered, given the data sample, that these were really version numbers, not decimal numbers, and sorted them accordingly. So that between 4.2 and 4.20, you might find, for example, 4.7, 4.8, and 4.18. This is again my ST solution with no change to the code except for the input data:
    use strict; use warnings; use feature "say"; my @array = qw(Patch_11.4 Patch_1.0 Patch_4.22 Patch_3.1 Patch_5.0 Pat +ch_4.2 Patch_6.0 Patch_4.8 Patch_4.7 Patch_4.20 Patch_9.3 Patch_10.2 Pat +ch_11.2 Patch_4.18); @array = map { $_->[0] } sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] } map { /Patch_(\d+)\.(\d+)/; [$_, $1, $2]} @array; say for @array;
    And the output:
    $ perl sort_versions.pl Patch_1.0 Patch_3.1 Patch_4.2 Patch_4.7 Patch_4.8 Patch_4.18 Patch_4.20 Patch_4.22 Patch_5.0 Patch_6.0 Patch_9.3 Patch_10.2 Patch_11.2 Patch_11.4
    Now, of course, this assumption about version numbers may be wrong, only the OP can clarify that with certainty.
      Not the ... solution I originally presented (Re: sorting an array with decimal points).

      Indeed, I should have noticed that the comparison was a multi-key numeric and therefor a version-number-ish comparison. I've added an appropriate update here.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2018-11-14 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (160 votes). Check out past polls.

    Notices?