简体   繁体   English

如何按 Powershell 中的一个属性值对对象数组进行排序?

[英]How do I sort an array of objects by one of their property values in Powershell?

For example, I have a variable, which returns the line with several arrays:例如,我有一个变量,它返回带有几个 arrays 的行:

@{sourceDSAcn=B; LastSyncResult=0} @{sourceDSAcn=A; LastSyncResult=9} @{sourceDSAcn=C; LastSyncResult=0} @{sourceDSAcn=M; Last SyncResult=10}

I want to sort this line alphabetically by one of parameters.我想按其中一个参数的字母顺序对这一行进行排序。 In this case - by sourceDSAcn , so result must be like that:在这种情况下 - 通过sourceDSAcn ,所以结果必须是这样的:

@{sourceDSAcn=A; LastSyncResult=9} @{sourceDSAcn=B; LastSyncResult=0} @{sourceDSAcn=C; LastSyncResult=0} @{sourceDSAcn=M; Last SyncResult=10}

How can I do that?我怎样才能做到这一点?

Your output format suggests two things:您的 output 格式表明两件事:

  • The objects aren't arrays , but custom objects ( [pscustomobject] instances).这些对象不是arrays ,而是自定义对象[pscustomobject]实例)。

  • You've used the Write-Host cmdet to print these objects to the host (display), which results in the hashtable-literal- like representation shown in your question (see this answer ).您已使用Write-Host cmdet 将这些对象打印到主机(显示),这导致在您的问题中显示类似哈希表文字的表示(请参阅此答案)。

    • If, instead, you want the usual rich display formatting you get by default - while still sending the output to the host only rather than to the success output stream - you can use the Out-Host cmdlet.相反,如果您想要默认获得的通常的丰富显示格式 - 同时仍将 output 仅发送到主机而不是成功 output stream - 您可以使用Out-Host cmdlet。
    • Conversely, to produce data output to the pipeline , use either the Write-Output cmdlet or, preferably, PowerShell's implicit output feature, as shown below;相反,要向管道生成数据output,请使用Write-Output cmdlet,或者最好使用 PowerShell 的隐式 output 功能,如下所示; for more information, see this answer .有关详细信息,请参阅此答案

In order to sort (custom) objects by a given property , simply pass the name of that property to为了按给定属性对(自定义)对象进行排序,只需将该属性的名称传递给
Sort-Object 's (positionally implied) -Property parameter, as Mathias R. Jessen helpfully suggests: Sort-Object的(位置隐含的) -Property参数,如Mathias R。Jessen有益地建议:

# Using $variable by itself implicitly sends its value through the pipeline.
# It is equivalent to: Write-Output $variable | ...
$variable | Sort-Object sourceDSAcn # same as: ... | Sort-Object -Property sourceDSAcn

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Powershell中,如何将对象数组的一个属性连接成一个字符串? - In Powershell, how do I concatenate one property of an array of objects into a string? 如何使用键的值对stdClass对象排序多维数组? - How do I sort a multidimensional array with stdClass Objects by values of a key? 如何基于另一个数组中的键/值对为一个数组中的对象分配属性值? - How do I assign property values for objects in one array based on key/value pairs in another? 如何按另一个数组中的相应值对一个数组进行排序? - How do I sort one array by the corresponding values in another array? 按一个属性对对象数组进行排序 - Sort array of objects by one property Select PowerShell 中数组的所有对象的一个属性的值 - Select the values of one property on all objects of an array in PowerShell 在数组中,如何使用.sort()根据一个属性将另一个属性排序的对象组合在一起? - In an array, how do you group together objects based on one property that are being sorted by another, using .sort()? 如何通过对象字符串值之一对对象数组进行数字排序? - How to sort an array of objects by one of the objects string values numerically? 如何根据属性值的 substring 对对象数组进行排序 - How to sort an array of objects based on the substring of property values 如何按属性值对对象数组进行排序并在该值相等时求和? - How to sort an array of objects by property values and sum when this value is equal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM