richie5um / vscode-sort-json

VSCode Extension to Sort JSON objects
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
MIT License
107 stars 20 forks source link

[BUG] The elements in an array aren't sorted #68

Closed cateyes99 closed 1 year ago

cateyes99 commented 1 year ago

Say I have a JSON doc as below

{
    "productInstanceIds": [
        "dee3265d-2d4a-47d3-d058-306f638391f3",
        "b48af1e0-ba0f-7dd4-ba36-f4810da88b75",
        "fc592fba-c453-e0b6-f2ba-b02a13c1c6de"
    ],
    "path": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd",
    "id": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd"
}

After running the command Sort JSON, I expect it results in the below one:

{
    "id": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd",
    "path": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd",
    "productInstanceIds": [
        "b48af1e0-ba0f-7dd4-ba36-f4810da88b75",
        "dee3265d-2d4a-47d3-d058-306f638391f3",
        "fc592fba-c453-e0b6-f2ba-b02a13c1c6de"
    ]
}

But the actual resulted one is this:

{
    "id": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd",
    "path": "22ccef63-5fbf-430c-a1db-6d4f28ff6ecd",
    "productInstanceIds": [
        "dee3265d-2d4a-47d3-d058-306f638391f3",
        "b48af1e0-ba0f-7dd4-ba36-f4810da88b75",
        "fc592fba-c453-e0b6-f2ba-b02a13c1c6de"
    ]
}

Note: The array productInstanceIds isn't sorted in the result! But the rest is sorted as expected.

richie5um commented 1 year ago

Hi. Yes, this is working as expected. SortJSON aims to make the JSON easier to read, but avoids making data modifications. Changing the order of an array is a data modification.

cateyes99 commented 1 year ago

@richie5um, apparently why I'm interested in sorted JSONs, one most important thing is that I'd like to compare JSONs, so sorting JSONs comes handy. What about offering a flag to include sorting arrays if you don't want it by default?