Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda