... 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.
| [reply] [d/l] [select] |
| [reply] [d/l] |