Let's say I have a collection of strings like this, in no particular order:
"Filename (Text) (Comment)"
"Filename (Word) (1)"
"Filename (Word) (1) (Bad)"
"Filename (Text) (2)"
"Filename (Picture)"
"Filename (Misc)"
"Filename (Misc) (1)"
"Filename (Picture) (1)"
"Filename (Audio)"
"Filename (Audio) (Comment) (1)"
I want to sort them like this:
"Filename (Text) (2)"
"Filename (Word) (1)"
"Filename (Text) (Comment)"
"Filename (Audio) (Comment) (1)"
"Filename (Audio)"
"Filename (Picture) (1)"
"Filename (Picture)"
"Filename (Misc) (1)"
"Filename (Misc)"
"Filename (Word) (1) (Bad)"
In other words, I want to sort in this order: (Bad) to the bottom (Text/Word, Audio, Picture) on top of the (Bad)s and then sort on (2, 1, [blank]).
Note that I want Text and Word clumped together and then sorted on the number in parenthesis, and I don't care about anything that doesn't match those, so I don't care about (Comment).
Currently I'm getting this:
"Filename (Word) (1)"
"Filename (Text) (2)"
"Filename (Text) (Comment)"
"Filename (Audio) (Comment) (1)"
"Filename (Audio)"
"Filename (Picture) (1)"
"Filename (Picture)"
"Filename (Misc) (1)"
"Filename (Misc)"
"Filename (Word) (1) (Bad)"
So I'm very close to getting what I want.
Here's what I'm doing:
$badExpression = { if ($_ -match '\((Bad)\)') { $matches[1] } }
$documentExpression = { if ($_ -match '\((Text|Word)\)') { $matches[1] } }
$soundExpression = { if ($_ -match '\((Audio)\)') { $matches[1] } }
$imageExpression = { if ($_ -match '\((Picture)\)') { $matches[1] } }
$numberExpression = { if ($_ -match '\((2|1)\)') { $matches[1] } }
"Filename (Text) (Comment)", "Filename (Word) (1)", "Filename (Word) (1) (Bad)", `
"Filename (Text) (2)", "Filename (Picture)", "Filename (Misc)", "Filename (Misc) (1)", `
"Filename (Picture) (1)", "Filename (Audio)", "Filename (Audio) (Comment) (1)" | Sort-Object `
@{Expression = $badExpression; Descending = $false}, `
@{Expression = $documentExpression; Descending = $true}, `
@{Expression = $soundExpression; Descending = $true}, `
@{Expression = $imageExpression; Descending = $true}, `
@{Expression = $numberExpression; Descending = $true}
I'm definitely misunderstanding how exactly my expressions are being applied to the sort. I have a hunch that maybe I have to do a sequence of Sort-Objects, but I can't really figure out what.
Just to draw the focus: The issue I have is that (Word) and (Text) are supposed to be sorted as if they were the same.
Edit: Okay I think I got the behavior I want now with this, a subtle change, see the -replace I added.
$badExpression = { if ($_ -match '\((Bad)\)') { $matches[1] } }
$documentExpression = { if ($_ -match '\((Text|Word)\)') { $matches[1] -replace ".*",'Document'} }
$soundExpression = { if ($_ -match '\((Audio)\)') { $matches[1] } }
$imageExpression = { if ($_ -match '\((Picture)\)') { $matches[1] } }
$numberExpression = { if ($_ -match '\((2|1)\)') { $matches[1] } }
"Filename (Text) (Comment)", "Filename (Word) (1)", "Filename (Word) (1) (Bad)", `
"Filename (Text) (2)", "Filename (Picture)", "Filename (Misc)", "Filename (Misc) (1)", `
"Filename (Picture) (1)", "Filename (Audio)", "Filename (Audio) (Comment) (1)" | Sort-Object `
@{Expression = $badExpression; Descending = $false}, `
@{Expression = $documentExpression; Descending = $true}, `
@{Expression = $soundExpression; Descending = $true}, `
@{Expression = $imageExpression; Descending = $true}, `
@{Expression = $numberExpression; Descending = $true}
So for internet points: What is my script doing (besides working), I'd like to understand it a little bit better. :) I understand what's happening loosely, but I'd like a better grasp of it.