I would like to sort semantic versions (semver.org)
v1.4.0 v1.4.0-alpha v1.4.0-alpha1 v1.4.0-patch v1.4.0-patch9 v1.4.0-patch10 v1.4.0-patch2 v1.5.0 v1.5.0-alpha v1.5.0-alpha1 v1.5.0-alpha2 v1.5.0-patch v1.5.0-patch1
in proper way. For instance, as version_compare()
does in PHP (it doesn't directly, but can be used for that).
Of course, sort -V|--version-sort
doesn't work here.
$ echo 1.0 1.0-alpha | tr ' ' "\n" | sort --version-sort 1.0 1.0-alpha
Is there some exist approach?
P.S.
In common sense, it should follow this schema:
1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0 < 1.0.0-patch < 1.0.0-patch.1.
P.P.S.
Semver 2.0 doesn't support patches, but it's needed.
If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.
-r Option: Sorting In Reverse Order: You can perform a reverse-order sort using the -r flag. the -r flag is an option of the sort command which sorts the input file in reverse order i.e. descending order by default. Example: The input file is the same as mentioned above.
Semantic versioning is a formal convention for determining the version number of new software releases. The standard helps software users to understand the severity of changes in each new distribution. A project that uses semantic versioning will advertise a Major, Minor and Patch number for each release.
Semantic Versioning is a versioning scheme for using meaningful version numbers (that's why it is called Semantic Versioning). Specifically, the meaning revolves around how API versions compare in terms of backwards-compatibility.
Sorting an array of semantic versions. If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.
If you find yourself having to sort using semantic version strings, (sorting a list of releases chronologically, say), we first need to split the string into its values and then compare each value. In this case we would sort by major release first, then minor, then finally by patch number.
The sort command comes with 31 options (13 main and 18 categorized as other). Most experienced bash programming (even experts) know only a few main sort options required to get by. Others are seldom touched. Lucky for you we have time to touch them all.
“Semantic versioning” is a system of versioning software, whereby the version is defined by three numbers: Major release number, minor release number, and patch release number. These values come in string form as “MAJOR.MINOR.PATCH”, so “5.2.1” means the fifth major release, second minor release, first patch.
Well, we could trick sort -V
by adding a dummy character at the end of the string for lines that do not contain a hyphen:
$ echo "$versions" | sed '/-/!{s/$/_/}' | sort -V | sed 's/_$//' v1.4.0-alpha v1.4.0-alpha1 v1.4.0-patch v1.4.0-patch2 v1.4.0-patch9 v1.4.0-patch10 v1.4.0 v1.5.0-alpha v1.5.0-alpha1 v1.5.0-alpha2 v1.5.0-patch v1.5.0-patch1 v1.5.0
Underscore lexically sorts after hyphen. That's the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With