I have a script that contains an array of alphanumerical strings like VER11.10.00.000
, VER11.10.01.123
and VER9.09.02.050
.
I sort this array like this
[string[]] $HighestVER = $Version | Sort -Descending
foreach($element in $HighestVER) {
Write-Host $element
}
$Version
represents an un-ordered array of the strings.
When running the script the outcome looks like this:
VER9.16.00.000
VER9.15.00.000
VER9.14.00.000
VER9.13.00.000
VER11.9.00.000
VER11.8.00.000
VER11.7.00.000
VER11.6.00.000
VER11.5.00
VER11.4.00.000
VER11.3.00.016
VER11.3.00.000
VER11.2.00.000
VER11.10.00.000
As you can see the sort is doing something, but it doesn't do it as I expected. My expected output is:
VER11.10.00.000
VER11.9.00.000
VER11.8.00.000
VER11.7.00.000
VER11.6.00.000
VER11.5.00
VER11.4.00.000
VER11.3.00.016
VER11.3.00.000
VER11.2.00.000
VER9.16.00.000
VER9.15.00.000
VER9.14.00.000
VER9.13.00.000
How can I improve my code to match the expected output?
Edit:
I can't solve this problem with [System.Version]
because I have the alphabetical chars in my version. If I remove the first three chars VER
and do the comparison it kinda works, but the elemnts of the version get messed up.
[string[]] $HighestVER = $Version2 | ForEach-Object { [System.Version] $_ } | Sort-Object -Descending | ForEach-Object { $_.toString() }
#[string[]] $HighestVER = $Version | Sort -Descending
foreach($element in $HighestVER) {
$element = "VER" + $element
Write-Host $element
}
which gives me this output:
VER11.10.0.0
VER11.9.0.0
VER11.8.0.0
VER11.7.0.0
VER11.6.0.0
VER11.5.0
VER11.4.0.0
VER11.3.0.16
VER11.3.0.0
VER11.2.0.0
VER9.16.0.0
VER9.15.0.0
VER9.14.0.0
VER9.13.0.0
The highest version no. is used for comparison and extending the version list (Dynamics NAV automatic build). Because of that the format hast to be identical.