user1151482
user1151482

Reputation: 13

Enumerate through reverse ordered array with powershell

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.

Upvotes: 0

Views: 386

Answers (1)

Erik Blomgren
Erik Blomgren

Reputation: 886

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

Upvotes: 1

Related Questions