Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda