1

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?

1 Answer 1

2

You can cast the string to an integer:

$PortExceptionArray = $PortException -split ',' | Sort-Object -Property {[int]$_ }

Output:

53
3128
3389
5985
5986
Sign up to request clarification or add additional context in comments.

2 Comments

This works great, thanks a lot for the quick answer:)
Or for short: [int[]]$PortException.Split(',') | Sort-Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.