在Powershell中,可以使用Hashtable类来创建哈希表。哈希表中的键和值可以是任意类型的数据。要排序包含数组的哈希表,可以使用Sort-Object cmdlet来对哈希表中的键进行排序。 下面是一个示例代码,演示如何创建一个包含数组的哈希表,并对其进行排序: 代码语言:txt 复制 # 创建包含数组的哈希表 $hash
Describes how to create, use, and sort hashtables in PowerShell. Long description A hashtable, also known as a dictionary or associative array, is a compact data structure that stores one or more key-value pairs. For example, a hash table might contain a series of IP addresses and com...
Tony =30Rony =40Sam =35}$HT.ContainsKey("Tony")$HT.ContainsValue(33)# Removing Key Values from Hashtable$HT.Remove("Rony")$HT$HT.Clear()$HT### Custom Colom Name$HT$HT|ftname,@{Name ="age"; expression = {$_.Value}}## Sorting HT$HT|Sort-Object-PropertyName$HT.GetEnumerator() ...
如果集合中有你想要按照它来排序的字段,排序是非常容易的,你可以把数据添加到对象里或者使用Sort-Object来创建一个自定义排序表达式 Get-ADUser|Sort-Object-Parameter@{ e={Get-TotalSales$_.Name } } 在这个示例中,我取出了用户集合并使用自定义命令获取额外信息用来排序. 如果你有一个hashtable的列表(list)想来...
Get-ADUser|Sort-Object-Property@{ e={Get-TotalSales$_.Name } } 在本例中,我将使用一列用户,并使用一些自定义 cmdlet 获取更多有关排序的信息。 对一列哈希表排序 如果你有一列想要排序的哈希表,你会发现Sort-Object不会将键视为属性。 我们可以通过使用自定义排序表达式来解决。
PS (11) > $user[[string[]] ($user.keys | sort)] John Smith 555-1212 You’ll notice something funny about the last example: we had to cast or convert the sorted list into an array of strings. This is because the hashtable keys mechanism expects strings, not objects, as keys. There...
虽然无法对哈希表进行排序,但可以使用哈希表的GetEnumerator()方法来枚举键和值,然后使用Sort-Objectcmdlet 对枚举值进行排序以显示。 例如,以下命令枚举$p变量中哈希表中的键和值,然后按字母顺序对键进行排序。 PowerShell PS>$p.GetEnumerator() |Sort-Object-PropertyKey Name Value --- --- Hash2 {[a,1]...
and munching on a kiwi. I do not have enough time to make my usual bowl of Irish steel-cut oats this morning. While I wait for my tea to steep, I thought I would answer a question that I have received several times over the last few days, “How do I sort a hash table in Windo...
哈希表的嵌套使用可以让表的层次结构更清晰,就像多维数组一样。 譬如我要录入某个账号的属性,其中其地址需细分: 六、给对象和哈希表进行排序 如果要完成主要关键字降序,次要关键字升序的排序: Dir | Sort-Object@{expression="Length";Descending=$true},@{expression="Name";Ascending=$true}...
Hashtables are inherently unsorted, but when you’re printing a hashtable’s contents to the console, it can certainly be helpful to sort its contents by key. Although it’s not obvious, the way to do it is pretty easy.Let’s start with defining a hashtable and play with it ...