-1

I am trying to sort version numbers descendingly, however versions like "W2018.1.10" are below versions like "W2018.1.2" despite 10 being bigger than 2.

$request = 'place from where I pull the version list'
$content = Invoke-RestMethod -Uri $request -Method GET

$versionarray = $content.versionlist[0].Versionen -split ", "
$sortedArray = $versionArray + "W2018.2.10.1" | sort -Descending
Write-Output $sortedArray

The current array looks like this:

W2019.2.8.7
W2019.2.8.6
W2019.2.8.5
W2019.2.8.3
W2018.2.10.1

It's supposed to look like this:

W2018.2.10.1
W2019.2.8.7
W2019.2.8.6
W2019.2.8.5
W2019.2.8.3
Supadugu
  • 133
  • 1
  • 1
  • 6
  • 2
    Why? You are sorting (alphabetically) descending and 2018 is smaller than 2019. –  Apr 04 '19 at 15:05
  • 1
    Possible duplicate of [How to sort by file name the same way Windows Explorer does?](https://stackoverflow.com/questions/5427506/how-to-sort-by-file-name-the-same-way-windows-explorer-does) – vonPryz Apr 04 '19 at 15:24
  • @LotPings Small oversight I made, sorry. But even then, 2.10 is still sitting at the bottom. – Supadugu Apr 04 '19 at 15:31
  • Of course, that is the same line with 2018... I did reformat your question to make that obvious. To sort correctly either convert/cast to version type or use `$ToNatural` in an answer to the question linked by @vonPryz I use quite often. –  Apr 04 '19 at 16:09

2 Answers2

0

Very dirty :)

    $x2 = @("W2019.2.8.7","W2019.2.9.7","W2018.2.1.7","W2020.1.1.7")

$hash = @{}

foreach ($x in $x2)

{

    $x -match "(w\d*\.)(\d*.\d*.\d*)"

$property = [ordered]@{

    'fullname' =  $matches[0]
    'sort' = $matches[2]
    }
    $res = New-Object -TypeName psobject -Property $property

$hash.add($res.fullname,$res.sort)
}

$res2 = ($hash |Sort-Object -Property value).Keys

$res2
Adam
  • 52
  • 3
  • That outputs four times true and is still an alphabetic sort which fails if the number of places varies, ordering (1,10,2,20 etc.). –  Apr 04 '19 at 16:20
0

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