compareVersions
public static int compareVersions(String version1,
String version2)
This function splits the two versions on "." and performs a
naturally-ordered comparison of the resulting components. For example, the
version string "0.3" is considered to precede "0.20", despite the fact that
lexical comparison would consider "0.20" to precede "0.3". This method of
comparison is similar to the method used by package versioning systems like
deb and RPM.
Version components are compared numerically whenever possible, however a
version component can contain non-numeric characters. When a non-numeric
group of characters is found in a version component, this group is compared
with the similarly-indexed group in the other version component. If the
other group is numeric, then the numeric group is considered to precede the
non-numeric group. If both groups are non-numeric, then a lexical
comparison is performed.
If two versions have a different number of components, then only the lower
number of components are compared. If those components are identical
between the two versions, then the version with fewer components is
considered to precede the version with more components.
In addition to the above rules, there is one special case: maven SNAPSHOT
releases are considered to precede a non-SNAPSHOT release with an
otherwise identical version number. For example, 2.0-SNAPSHOT precedes
2.0.
This function returns a negative integer if version1 precedes version2, a
positive integer if version2 precedes version1, and 0 if and only if the
two versions' components are identical in value and cardinality.
- Parameters:
version1
- the first version to compareversion2
- the second version to compare
- Returns:
- a negative integer if version1 precedes version2, a positive
integer if version2 precedes version1, and 0 if and only if the two
versions are equal.