Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created September 5, 2024 01:49
Show Gist options
  • Save steviecoaster/f25d7bff7844309405f2de7949e7f1f1 to your computer and use it in GitHub Desktop.
Save steviecoaster/f25d7bff7844309405f2de7949e7f1f1 to your computer and use it in GitHub Desktop.
Reorder items in a collection
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