shifting an array more than once.

I want to put the two sundays at the end of the array.

$array = @()
$array += "sunday"
$array += "sunday"
$array += "monday"
$array += "tuesday"

at this point, $array[0] is just sunday.

so the following will move the first sunday:

    $first,$rest = $Array
    $Array = $rest,$first

at this point, $array[0] is
sunday
monday
Tuesday

so if I now shift it again with the same commands, it just moves those three below the first sunday, aka moving the first sunday back to the top of the list.
is there something I have to do after a shift to make the array ‘normal’ again, so that $array[0] is just the first line of the array?

Your last line should read

$Array = $rest + $first

instead of

$Array = $rest , $first
PS D:\Sandbox> $array = @()
$array += 'sunday'
$array += 'sunday'
$array += 'monday'
$array += 'tuesday'


$first,$rest = $array
$array = $rest + $first

$first,$rest = $array
$array = $rest + $first

$array
monday
tuesday
sunday
sunday

What is it what you actually try to do in the end? Arrays are actually immutable. With the operator += you actually use the “old” array and a new element to create a “new” array with the content of the old plus the new element. If you need more felxibility you should use arraylists.
You might change your line of code from this

$Array = $rest,$first
to this
Array = ($rest.GetEnumerator()), $first
to achieve what I think you wanted.

Thanks guys. The notifications for your replies went to my junk folder, and I’ve since solved my issue by creating a Monday to Sunday enum and sorting my array by it.

PowerShell uses Sunday as the first day of the week day zero, so sorting by the [int] [dayofweek] value wasn’t working for me. I needed Monday to be first. I tried shifting Sundays to the end with a do/while loop, but ran into the issue in the original post.

I’m glad you figured it out. Enums are a great way to solve that issue. I’m assuming your Enum looks something like this:

Enum MyDays
{
 Mon = 0
 Tue = 1
 Wed = 2
 Thu = 3
 Fri = 4
 Sat = 5
 Sun = 6
}
$array += $element

is not the most efficient way to add to an array. You’re basically copying the whole array and making a new one with an extra element.

I’m following a github thread that just mentioned it: https://github.com/PowerShell/PowerShell/issues/6745#issuecomment-480457623

@Russell yep, but then I was told actually didn’t even need the “= [integer]” part if the enum lists the days in the order I wanted. It will automatically assign an index value based on the order