Sorting PowerShell versions

后端 未结 5 539
無奈伤痛
無奈伤痛 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:13

    A version string can be cast to a Version object, and sort-object can be passed a script block and sort on the result.

    PS C:\Users\me> "3.11.0.1", "3.0.1.1", "3.2.1.1" | sort
    3.0.1.1
    3.11.0.1
    3.2.1.1
    
    PS C:\Users\me> "3.11.0.1", "3.0.1.1", "3.2.1.1" | sort {[version] $_}
    3.0.1.1
    3.2.1.1
    3.11.0.1
    

    (Added an extra version string to make the example actually meaningful.)

提交回复
热议问题