1Sort::Versions(3) User Contributed Perl Documentation Sort::Versions(3)
2
3
4
6 Sort::Versions - a perl 5 module for sorting of revision-like numbers
7
9 use Sort::Versions;
10 @l = sort { versioncmp($a, $b) } qw( 1.2 1.2.0 1.2a.0 1.2.a 1.a 02.a );
11
12 ...
13
14 use Sort::Versions;
15 print 'lower' if versioncmp('1.2', '1.2a') == -1;
16
17 ...
18
19 use Sort::Versions;
20 %h = (1 => 'd', 2 => 'c', 3 => 'b', 4 => 'a');
21 @h = sort { versioncmp($h{$a}, $h{$b}) } keys %h;
22
24 Sort::Versions allows easy sorting of mixed non-numeric and numeric
25 strings, like the 'version numbers' that many shared library systems
26 and revision control packages use. This is quite useful if you are
27 trying to deal with shared libraries. It can also be applied to
28 applications that intersperse variable-width numeric fields within
29 text. Other applications can undoubtedly be found.
30
31 For an explanation of the algorithm, it's simplest to look at these
32 examples:
33
34 1.1 < 1.2
35 1.1a < 1.2
36 1.1 < 1.1.1
37 1.1 < 1.1a
38 1.1.a < 1.1a
39 1 < a
40 a < b
41 1 < 2
42 1.1-3 < 1.1-4
43 1.1-5 < 1.1.6
44
45 More precisely (but less comprehensibly), the two strings are treated
46 as subunits delimited by periods or hyphens. Each subunit can contain
47 any number of groups of digits or non-digits. If digit groups are being
48 compared on both sides, a numeric comparison is used, otherwise a ASCII
49 ordering is used. A group or subgroup with more units will win if all
50 comparisons are equal. A period binds digit groups together more
51 tightly than a hyphen.
52
53 Some packages use a different style of version numbering: a simple real
54 number written as a decimal. Sort::Versions has limited support for
55 this style: when comparing two subunits which are both digit groups, if
56 either subunit has a leading zero, then both are treated like digits
57 after a decimal point. So for example:
58
59 0002 < 1
60 1.06 < 1.5
61
62 This won't always work, because there won't always be a leading zero in
63 real-number style version numbers. There is no way for Sort::Versions
64 to know which style was intended. But a lot of the time it will do the
65 right thing. If you are making up version numbers, the style with
66 (possibly) more than one dot is the style to use.
67
69 The function "versioncmp()" takes two arguments and compares them like
70 "cmp". With perl 5.6 or later, you can also use this function directly
71 in sorting:
72
73 @l = sort versioncmp qw(1.1 1.2 1.0.3);
74
75 The function "versions()" can be used directly as a sort function even
76 on perl 5.005 and earlier, but its use is deprecated.
77
79 version, CPAN::Version which is part of the CPAN distribution.
80
82 <https://github.com/neilb/Sort-Versions>
83
85 Ed Avis <ed@membled.com> and Matt Johnson <mwj99@doc.ic.ac.uk> for
86 recent releases; the original author is Kenneth J. Albanowski
87 <kjahds@kjahds.com>. Thanks to Hack Kampbjørn and Slaven Rezic for
88 patches and bug reports.
89
91 This software is copyright (c) 1996 by Kenneth J. Albanowski.
92
93 This is free software; you can redistribute it and/or modify it under
94 the same terms as the Perl 5 programming language system itself.
95
96
97
98perl v5.32.0 2020-07-28 Sort::Versions(3)