1. 程式人生 > >PowerShell對象排序(結合哈希表)

PowerShell對象排序(結合哈希表)

asc 圖片 child -c com ssi sort etime orm

可以通過使用哈希表數組中對不同屬性進行不同的順序進行排序。

Get-ChildItem |
  Sort-Object -Property @{ Expression = ‘LastWriteTime‘; Descending = $true }, @{ Expression = ‘Name‘; Ascending = $true } |
  Format-Table -Property LastWriteTime, Name

為了提高可讀性,可以將哈希表放到一個單獨的變量:

$order = @(
  @{ Expression = ‘LastWriteTime‘; Descending = $true }
  @{ Expression = ‘Name‘; Ascending = $true }
)

Get-ChildItem |
  Sort-Object $order |
  Format-Table LastWriteTime, Name

哈希表中進行排序的鍵可縮寫如下所示:

Sort-Object @{ e = ‘LastWriteTime‘; d = $true }, @{ e = ‘Name‘; a = $true }

您也可以關註下方微信公眾號獲取更多資訊
技術分享圖片

PowerShell對象排序(結合哈希表)