Would like to convert a string with collection of numbers into a sorted array object in PowerShell. However, it was sorted based on the ASCII value of each character, but not the value of number. For example: I have
[string]$PortException = "3128,53,3389,5985,5986"
When sort with Sort-Object:
$PortExceptionArray = ($PortException.Split(',') | Sort-Object)
The outcome to the array is in the order of:
3128
3389
53
5985
5986
However, it is expect that "53" will be on the top.
Any thoguht?
CodePudding user response:
You can cast the string to an integer:
$PortExceptionArray = $PortException -split ',' | Sort-Object -Property {[int]$_ }
Output:
53
3128
3389
5985
5986