Sorting a 2D array with one row

后端 未结 1 731
长情又很酷
长情又很酷 2021-01-28 16:59

I\'ve recently started scripting in PowerShell and I\'m having a difficulty in sorting a 2D array by multiple columns.

The following code is working fine. I have a 2D ar

相关标签:
1条回答
  • 2021-01-28 17:31

    Sort-Object does not return array. It return write to pipeline individual input items in sorted order.

    PS> 7,3,8,5,1 | Sort-Object | ForEach-Object { Write-Host "Item: $_" }
    Item: 1
    Item: 3
    Item: 5
    Item: 7
    Item: 8
    

    As you can see, next command in pipeline ForEach-Object see each individual item, but not array as whole.

    Wrapping to array in $table1 case happens, because last command in pipeline wrote more than one item in pipeline. In $table2 case last command in pipeline wrote only one item, so no wrapping necessary there. If you want to always wrap pipeline results into array regardless of amount of results (zero, one or many), then you need to use array subexpression operator:

    $Results = @( First-Command | Middle-Command | Last-Command )
    
    0 讨论(0)
提交回复
热议问题