richie5um / vscode-sort-json

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

Sort by specific key value #18

Closed alystair closed 5 years ago

alystair commented 6 years ago

I think it'd be awesome to be able to sort by a specific key value that's within each object, should just abort if it's missing from even one object that's selected. Eg. Dsc by 'name' would put Bob first.

[{
"name":"Debbie",
"age":30
},{
"name":"Bob",
"age":50
}]
richie5um commented 6 years ago

Thanks. I like the idea, but that is a different kind of sort - as it changes the array order. This extension is focused on avoiding any changes in the data (ie, no index changes).

I think I should be able to create something fairly quickly. Leave it with me :-).

alystair commented 6 years ago

That's interesting, I never considered using the actual response index for anything in my own developments since the data I normally deal with usually has some metadata in regards to rank or ordering.Looking forward to it, feel free to ping me if you need a test subject! From: notifications@github.comSent: May 28, 2018 4:52 AMTo: vscode-sort-json@noreply.github.comReply to: reply@reply.github.comCc: alystair@gmail.com; author@noreply.github.comSubject: Re: [richie5um/vscode-sort-json] Sort by specific key value (#18) Thanks. I like the idea, but that is a different kind of sort - as it changes the array order. This extension is focused on avoiding any changes in the data (ie, no index changes). I think I should be able to create something fairly quickly. Leave it with me :-).

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or mute the thread.

ldexterldesign commented 5 years ago

Real life use case for this I've been meaning to solve myself...

I'm using lines of JSON ala [J]SONLines but need to control key/property order because in some cases I'm sorting the lines alphanumerically in order to make sense of the data (e.g. ranking, scoring)

Here I want score to be the first key/property so I can create a primitive league table

Before:

{"name": "bar", "score": 5},
{"name": "baz", "score": 2},
{"name": "foo", "score": 7}

After:

{"score": 7, "name": "foo"},
{"score": 5, "name": "bar"},
{"score": 2, "name": "baz"}

Hope this helps

Good luck!

j: http://jsonlines.org/examples/

richie5um commented 5 years ago

You can use the settings values to override the sort order...

image
ldexterldesign commented 5 years ago

@richie5um

Thanks for reply

Care to elaborate with a simple example?

Unsure of settings.json capabilities (e.g. hooks?)...

Yours hopefully

richie5um commented 5 years ago

HTH...

Given this JSON:

[
    {
        "name": "bar",
        "score": 5
    },
    {
        "name": "baz",
        "score": 2
    },
    {
        "name": "foo",
        "score": 7
    }
]

If you sort that, 'name' comes before 'score'.

Edit your preferences in VSCode (i.e. settings.json) image

And add this to it:

    "sortJSON.orderOverride": [
        "score"
    ]

Now, if you run sortJSON on that original JSON, it'll sort 'score' first in the object properties.

[
    {
        "score": 5,
        "name": "bar"
    },
    {
        "score": 2,
        "name": "baz"
    },
    {
        "score": 7,
        "name": "foo"
    }
]