The semver Nushell plugin can be used to work with string values as semver type as you can see in a previous post.
You can use the semver sort command to sort string values with ordering rules for semantic versions.
With natural ordering of string values a value of 10.0.1 is placed before 2.1.0, but if you use semver sort the ordering will be correct.
The command will look at all the parts of the semver type.
So a major version of 2 is placed before 10.
If the major version part is the same than the minor part is used for ordering and so on.
To sort in descending order you can use the option --reverse or the short option -r.
In the following example a list of version values is sorted with the semver sort command:
use std/assert
# After installing the nu_plugin_semver plugin and
# adding it to the plugin registry you can use
# the plugin for Nushell code.
plugin use semver
# `semver sort` uses correct ordering for version values.
(assert equal
(['10.0.2' '2.3.1' '1.10.1' '2.3.1-beta.3' '2.3.1-alpha.1'] | semver sort)
['1.10.1'
'2.3.1-alpha.1'
'2.3.1-beta.3'
'2.3.1'
'10.0.2'])
# Without `semver sort` the natural ordering is used, but that is not correct
# for version values. E.g. 10.0.2 is before 2.3.1.
(assert equal
(['10.0.2' '2.3.1' '1.10.1' '2.3.1-beta.3' '2.3.1-alpha.1'] | sort)
['1.10.1'
'10.0.2'
'2.3.1'
'2.3.1-alpha.1'
'2.3.1-beta.3'])
# Using the option `--reverse` (shorthand `-r`) to reverse the sorting order.
(assert equal
(['10.0.2' '2.3.1' '1.10.1' '2.3.1-beta.3' '2.3.1-alpha.1'] | semver sort --reverse)
['10.0.2'
'2.3.1'
'2.3.1-beta.3'
'2.3.1-alpha.1'
'1.10.1'])
Written with Nushell 0.111.0.