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.
CodePudding user response:
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:
cpe:2.3:h:hp
cpe:2.3:o:juniper:junos
cpe:2.3:a:microsoft:sql_server