Sort Hashtable and assign it to a new variable in Powershell
问题 I would like to know how to assign a sorted hasttable using the following command to a new variable. I am using the following command $ht = @{ 1 = "Data1"; 3 = "Data3"; 2 = "Data2"; } $htSorted = $ht.GetEnumerator() | Sort-Object Name but after running it i can't traverse on the resultant hashtable foreach($key in $htSorted.Keys) { $item[$key] } Any idea ? 回答1: In case you need to have a sorted hashtable, you can use SortedDictionary . Example: $s = New-Object 'System.Collections.Generic