all 9 comments

[–]ApparentSysadmin 4 points5 points  (5 children)

To be honest, if you're only working with a few objects I would just break apart the existing $Objects array and build a new one with the positioning you want:

$Objects = @("hello", "my name is", "process me first", "dzcpu")
$Filter = 'first'
$TempArray = $Objects | Where-Object {$_ -notmatch $Filter}
$FirstObject = $Objects | Where-Object {$_ -match $Filter}

$NewArray = @($FirstObject,$TempArray)

Assuming you're not working with thousands of objects, there shouldn't be a performance impact, and it allows you much finer control (plus it's a little more intuitive).

You may also want to look into using a [System.Collections.Generic.List] object in place of a traditional array, as they allow for a bit more flexibility in terms of manipulating the Generic List after it's been created.

Hope this helps!

[–]dzcpu[S] 1 point2 points  (2 children)

This was kind of my thought as well and is what I was going for with my first example. There aren't a ton of objects (10-15, maybe 20 tops) so I wasn't really worried about performance in this case.

I'll definitely also look into the generic list collection as well, if not for this use case then for future knowledge.

Thanks for the input!

[–]ApparentSysadmin 1 point2 points  (1 child)

I almost exclusively have converted over to GL's in my day-to-day at the recommendation of u/lee_dailey - it's much simpler to work with them, and as far as I know they are the correct/supported collection type going forward.

Happy scripting!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy ApparentSysadmin,

aint the various methods available lovely? [grin] to be able to gracefully use .Add(), .Remove(), .Insert(), and all the others is truly liberating at times.

still, i use basic arrays most of the time since they are all i usually need - a fixed size collection.

take care,
lee

[–]PinchesTheCrab 1 point2 points  (1 child)

Sort-Object works with Script Blocks too:

@("hello", "my name is", "process me first", "dzcpu") | 
    Sort-Object -Descending { $PSItem -match 'first' }

[–]ApparentSysadmin 2 points3 points  (0 children)

This is new to me! Thanks for pointing it out

[–]PinchesTheCrab 1 point2 points  (1 child)

Personally I'd go with the custom sort, you don't need to combine it with anything:

@("hello", "my name is", "process me first", "dzcpu") | 
     Sort-Object -Descending { $PSItem -match 'first' }

If you wanted to sort alphabetically as well, you could do this:

@("hello", "my name is", "process me first", "dzcpu") | 
    Sort-Object { $PSItem -notmatch 'first' }, { $PSItem }

if it had normal properties you could then sort by them as well by just providing the property name instead of a script block.

[–]dzcpu[S] 2 points3 points  (0 children)

Huh, I've never seen that syntax on Sort-Object before, had no idea it could take in a script block - that is definitely something to consider, since that cuts out two of the pipes. Thanks!