To illustrate several methods:
## Q:\Test\2019\04\04\SO_55519038.ps1
## $ToNatural from Roman Kuzmin source <https://stackoverflow.com/a/5429048/6811411>
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20,"0") }) }
$versionarray = ("W2018.2.1.7",
"W2019.2.8.7",
"W2019.2.9.7",
"W2019.2.10.7",
"W2019.2.1.7")
"`nCasting all digits to version type"
$versionarray | ForEach-Object { [version]($_ -replace '^W')} | Sort
"`nSorting with `$ToNatural`n"
$versionarray | Sort $ToNatural
"`nBuilding a [PSCustomObject] with last 3 dot seperated numbers cast to version"
$versionarray | ForEach-Object {
[PSCustomObject]@{
VersionString = $_
Version = [version]($_ -split '\.',2)[1]
}
} | Sort Version,VersionString | ft
Sample output:
> Q:\Test\2019\04\04\SO_55519038.ps1
Casting all digits to version type
Major Minor Build Revision
----- ----- ----- --------
2018 2 1 7
2019 2 1 7
2019 2 8 7
2019 2 9 7
2019 2 10 7
Sorting with $ToNatural
W2018.2.1.7
W2019.2.1.7
W2019.2.8.7
W2019.2.9.7
W2019.2.10.7
Building a [PSCustomObject] with last 3 dot seperated numbers cast to version
VersionString Version
------------- -------
W2018.2.1.7 2.1.7
W2019.2.1.7 2.1.7
W2019.2.8.7 2.8.7
W2019.2.9.7 2.9.7
W2019.2.10.7 2.10.7