Created
September 5, 2024 01:49
-
-
Save steviecoaster/f25d7bff7844309405f2de7949e7f1f1 to your computer and use it in GitHub Desktop.
Reorder items in a collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Set-CollectionOrder { | |
| [CmdletBinding()] | |
| Param( | |
| [Parameter(Mandatory)] | |
| [System.Collections.Generic.List[string]] | |
| $Collection, | |
| [Parameter(Mandatory)] | |
| [String] | |
| $Item, | |
| [Parameter(Mandatory)] | |
| [Int] | |
| $Position | |
| ) | |
| process { | |
| $i = $Collection | Where-Object { $_ -match "$Item" } | |
| if ($Collection.Contains($i)) { | |
| $null = $Collection.Remove($i) | |
| $null = $Collection.Insert($Position - 1, $i) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment