Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
Sorting JSON by key

I have this JSON string:

{
  "name": ".",
  "_type": "folder",
  "children": [
    {
      "name": "Beverages",
      "_type": "folder",
      "children": [
        {
          "name": "Water",
          "_type": "file"
        },
        {
          "name": "Tea",
          "_type": "folder",
          "children": [
            {
              "name": "White tea",
              "_type": "file"
            },
            {
              "name": "Green tea",
              "_type": "folder",
              "children": [
                {
                  "name": "Sencha",
                  "_type": "file"
                },
                {
                  "name": "Pi Lo Chun",
                  "_type": "file"
                },
                {
                  "name": "Matcha",
                  "_type": "file"
                },
                {
                  "name": "Gyokuro",
                  "_type": "file"
                }
              ]
            },
            {
              "name": "Black tea",
              "_type": "file"
            }
          ]
        },
        {
          "name": "Coffee",
          "_type": "file"
        }
      ]
    }
  ]
}

It represents this folder:

│
└───Beverages
    │   Coffee
    │   Water
    │
    └───Tea
        │   Black tea
        │   White tea
        │
        └───Green tea
                Gyokuro
                Matcha
                Pi Lo Chun
                Sencha

The problem is that the JSON is ordered by the anti-lexicographical order: you can see Water-Tea-Coffee under Beverages. That is, it rather represents this structure:

.
|
`- Beverages
   |
   +- Water
   |
   +- Tea
   |  |
   |  +- White tea
   |  |
   |  +- Green tea
   |  |  |
   |  |  +- Sencha
   |  |  |
   |  |  +- Pi Lo Chun
   |  |  |
   |  |  +- Matcha
   |  |  |
   |  |  `- Gyokuro
   |  |
   |  `- Black tea
   |
   `- Coffee

But Tea is a folder, while Water and Coffee are files (they don't have children). I'd like to have the folders following by the files, as in the first tree picture.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!