2

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.

1
  • To clarify, you want to sort on the property id? Are you able to export the object using Export-CliXml and share it with us? Commented Apr 22, 2022 at 11:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that did work fine. I tried to select the innercomponent only in Sort-Object without success. I would have tried your solution anyway, if it would not have worked directly. The original structure was changed, but I will handle this. Now value array is lost, but I can access the components without value.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.