powershell sort-object pscustomObject带有对象[]作为值
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有一个示例工作数据集,很难为您的用例创建一个绝对的工作解决方案,但是您可以使用以下数据对数据进行排序:
这将返回“直接属性”下的所有对象
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:
This would return all of the objects under the immediate property
value
and sorted on the propertyid
.如果您想保留整个对象的当前结构:
:
必须应用
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:
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.