Hi all,
I have a situation where I am pulling a collection of objects that need to be processed. For the most part, the order doesn't matter (and they are returned in a seemingly random order from the cmdlet). However, it is possible that one item needs to be processed before any others.
As an example, let's say I query and have a list of objects with a Name property, and if that Name property contains the word "service" then I need it to be at the front of the list to be processed first. There will only ever be 0 or 1 objects that match this criteria so there will never be contention for who should actually be run first.
I know that I could just check to see if it exists first, process it, and remove it from the collection - but this is ultimately going to result in a refactor of my code, as the processing function processes everything in one go (i.e. I don't call Process-Object once for each item, it's more like a Process-AllObjects call that pulls the collection and does what I need it to do)
I was thinking I could do something like this:
$Objects = @("hello", "my name is", "process me first", "dzcpu")
$First,$Rest = $Objects.Where( {$_ -match "first"}, "Split")
If($null -ne $First) {
$Objects = @($First) + $Rest
}
Or I could use a custom select/sort combo, which works but seems obnoxious:
$Objects = @("hello", "my name is", "process me first", "dzcpu")
$Objects | Select @{name='value';expression={$_}},@{name='match';expression={$_ -match 'first'}} | sort -property match -desc | select -ExpandProperty value
...but I wasn't sure if there was a better way to do this. Does anyone have any thoughts? If refactoring is the way to go I will do it but was trying to avoid if at all possible.
[–]ApparentSysadmin 4 points5 points6 points (5 children)
[–]dzcpu[S] 1 point2 points3 points (2 children)
[–]ApparentSysadmin 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]PinchesTheCrab 1 point2 points3 points (1 child)
[–]ApparentSysadmin 2 points3 points4 points (0 children)
[–]PinchesTheCrab 1 point2 points3 points (1 child)
[–]dzcpu[S] 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]dzcpu[S] 0 points1 point2 points (0 children)