Private/ConvertTo-JsonElementWithSortedArray.ps1

function ConvertTo-JsonElementWithSortedArray {
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [System.Text.Json.JsonElement]$Json
    )

    process {

        function Normalize([System.Text.Json.JsonElement]$Element) {
            switch ($Element.ValueKind) {
                Object {
                    $props = foreach ($p in $Element.EnumerateObject()) {
                        '"{0}":{1}' -f $p.Name.Replace('"','\"'), (Normalize $p.Value)
                    }
                    '{' + ($props -join ',') + '}'
                }

                Array {
                    $items = foreach ($item in $Element.EnumerateArray()) {
                        Normalize $item
                    }

                    '[' + (($items | Sort-Object) -join ',') + ']'
                }

                default {
                    $Element.GetRawText()
                }
            }
        }

        [System.Text.Json.JsonDocument]::Parse((Normalize $Json)).RootElement.Clone()
    }
}