Home > OS >  Enumerate through reverse ordered array with powershell
Enumerate through reverse ordered array with powershell

Time:06-13

I'm trying to enumerate through a basic array, ordered alphabetically in reverse.

If I sort descending, then it shows in the console as I require. But the sorting doesn't 'stick' to the array when I iterate.

$symbols = @("Banana","Orange","Apple","Mango","Strawberry")

$symbols | Sort -Descending

write-host ""

foreach ($symbol in $symbols) {
    write-host $symbol
}

Any ideas? Thanks, Simon.

CodePudding user response:

Using pipe to sort doesn't manipulate the actual object, only a representation of it. To actually alter the object you need to assign to it with an =-sign.

$symbols = $symbols | Sort -Descending
  • Related