2

My array contains cpe:2.3:o:juniper:junos, cpe:2.3:h:hp, and cpe:2.3:a:microsoft:sql_server. I'd like to alphabetically sort this array based on the 4th substring, juniper, hp, and microsoft in this case. Substrings are delimited by a colon :.

This question is very similar to this one, but I'm looking for a Powershell way to do this. I've tried creating a regex to sort, but that proved to be difficult.

1 Answer 1

3

Use the Sort-Object cmdlet with a calculated property that uses the -split operator to sort by the token of interest:

$array = 'cpe:2.3:o:juniper:junos', 'cpe:2.3:h:hp', 'cpe:2.3:a:microsoft:sql_server'

$array | Sort-Object { ($_ -split ':')[3] }

Output (sorted by the 4th field):

cpe:2.3:h:hp
cpe:2.3:o:juniper:junos
cpe:2.3:a:microsoft:sql_server
Sign up to request clarification or add additional context in comments.

Comments

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.