powershell sort-object pscustomObject带有对象[]作为值

发布于 2025-01-23 14:47:41 字数 1227 浏览 0 评论 0原文

我正在使用RESTAPI访问Azure。我正在获取构建ID的更改列表。

我想将对象类型与增加的更改集编号进行排序。

我收到的$ cansceet.getType()的类型:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

这是输出$ changeet

count value
----- -----
   50 {@{id=68.......

因此,我检查了值的类型$ conseft $ changeet.value.getType ()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

之后,我检查了一个元素的类型:

$changeset.value[0].GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

我确实尝试了sort-object升和下降,但它不会更改顺序:

$changeset = $changeset | sort-object { $_.value.id } -desc

我想保持结构因为不打破某事。组件内有很多属性。

I am using RestAPI access to azure. I am getting a list of changesets for a build ID.

I would like to sort the object type with increasing Changeset number.

The type I receive for $changeset.GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

This is the output $changeset:

count value
----- -----
   50 {@{id=68.......

Therefore, I checked the type of value $changeset.value.GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

And afterwards I checked the type of an element:

$changeset.value[0].GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

I did try Sort-Object with ascending and descending, but it does not change the order:

$changeset = $changeset | sort-object { $_.value.id } -desc

I would like to keep the structure as it is to not break something. There are lots of properties within the component.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2025-01-30 14:47:41

没有一个示例工作数据集,很难为您的用例创建一个绝对的工作解决方案,但是您可以使用以下数据对数据进行排序:

$changeset | Select-Object -ExpandProperty value | Sort-Object -Property id

这将返回“直接属性”下的所有对象value并进行排序属性id

Without an example working dataset, it's hard create an absolute working solution for your use case, however you could sort the data using the below:

$changeset | Select-Object -ExpandProperty value | Sort-Object -Property id

This would return all of the objects under the immediate property value and sorted on the property id.

花心好男孩 2025-01-30 14:47:41

如果您想保留整个对象的当前结构:

$changeset.value = @($changeset.value | Sort-Object -Descending -Property Id)

  • 必须应用 sort-object 呼叫.value属性,其中包含[pscustomobject]的数组您要通过其.id属性值进行排序。

  • 将命令包含在@(...) array-subexpression operator ,即使在以下情况下,即使在以下情况下,也将排序对象视为数组em>可能存在对象。

  • 将结果分配回$ canspet.value用新的,排序的数组代替原始数组。

If you want to retain the object as a whole with its present structure:

$changeset.value = @($changeset.value | Sort-Object -Descending -Property Id)

That is:

  • You must apply the Sort-Object call to the .value property, which contains the array of [pscustomobject] instances you want to sort by their .Id property values.

  • Enclosing the command in @(...), the array-subexpression operator, ensures that the sort objects are treated as an array, even if situationally only one object may be present.

  • Assigning the results back to $changeset.value replaces the original array with a new, sorted array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文