Sorting PowerShell versions

后端 未结 5 532
無奈伤痛
無奈伤痛 2020-12-10 12:34

In PowerShell, if I have a list of strings containing versions, \"3.0.1.1\", \"3.2.1.1\", etc., how can I sort it the way System.Version would sort it in C#?

5条回答
  •  攒了一身酷
    2020-12-10 13:14

    Just to add another corner case: powershell treats this single digit kind of version '2' as invalid. Have to add '.0' to the end to create the version object before sorting:

    if($version  -match '^\d$')
    {
      $version = $version + '.0'
    }
    New-Object System.Version $version
    

提交回复
热议问题