krasa / StringManipulation

IntelliJ plugin - https://plugins.jetbrains.com/plugin/2162
Apache License 2.0
694 stars 81 forks source link

JSON Sort -> Sort by key #221

Open simube opened 8 months ago

simube commented 8 months ago

Is your feature request related to a problem? Please describe. I have to sort big JSON-files with arrays. The array-entries should be sorted by a specific key

Describe the solution you'd like When I hit JSON sort, it should ask me for keys to sort with, e.g. "date, last_name, ..."

Describe alternatives you've considered I use VisualStudioCode which has support for this. But I'd rather sort directly in PHPStorm :)

Additional context none

arnaldop commented 8 months ago

@simube, can you provide at least 1 data sample (pre and post sorting)?

simube commented 8 months ago

OK, I'll try to give an example:

unsorted:

{
  "persons": [
    {
      "name": "Michael",
      "age": 30
    },
    {
      "name": "Andrew",
      "age": 42
    },
    {
      "name": "Simon",
      "age": 18
    },
    {
      "name": "Julia",
      "age": 33
    }
  ]
}

sorted with JSON sort:

{
  "persons": [
    {
      "age": 30,
      "name": "Michael"
    },
    {
      "age": 42,
      "name": "Andrew"
    },
    {
      "age": 18,
      "name": "Simon"
    },
    {
      "age": 33,
      "name": "Julia"
    }
  ]
}

(this just puts "age" before "name")

What I'd like to do:

sorted by key "name" (Andrew first, Simon last):

{
  "persons": [
    {
      "age": 42,
      "name": "Andrew"
    },
    {
      "age": 33,
      "name": "Julia"
    },
    {
      "age": 30,
      "name": "Michael"
    },
    {
      "age": 18,
      "name": "Simon"
    }
  ]
}
simube commented 8 months ago

There is this plugin for VS Code which does the task: https://github.com/fvclaus/vsc-sort-json-array So it would be nice to have this functionality in PHPStorm too :)