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

117
Visualizações
How to conditionally sort and right shift a JSON array?

I have a JSON array as follows :

[{
    "name": "ABC",
    "default": false,
    "details": [
        {
            "detail1": "value1"
        },
        {
            "detail2": "value2"
        }
    ]
},
{
    "name": "PQR",
    "default": false,
    "details": [
        {
            "detail3": "value3"
        },
        {
            "detail4": "value4"
        }
    ]
},
{
    "name": "XYZ",
    "default": true,
    "details": [
        {
            "detail5": "value5"
        },
        {
            "detail6": "value6"
        }
    ]
}]

Now if the default value is true , I want it to be the first element of the array and rest of the elements should be sorted alphabetically based on the name . Only one of the element will have default as true or all the elements will have default as false.

The expected JSON after sorting should be :

[{
    "name": "XYZ",
    "default": true,
    "details": [
        {
            "detail5": "value5"
        },
        {
            "detail6": "value6"
        }
    ]
},
{
    "name": "ABC",
    "default": false,
    "details": [
        {
            "detail1": "value1"
        },
        {
            "detail2": "value2"
        }
    ]
},
{
    "name": "PQR",
    "default": false,
    "details": [
        {
            "detail3": "value3"
        },
        {
            "detail4": "value4"
        }
    ]
}]

I have tried this code for sorting the JSON array :

jsonArray.sort( function( a, b ) {
    a = a.name.toLowerCase();
    b = b.name.toLowerCase();    
    return a < b ? -1 : a > b ? 1 : 0;        
});

But I am not able to figure out how to check for the default value and sort accordingly. How do I achieve the expected result?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're on the right way. Just modify the function to include the check for default:

jsonArray.sort( function( a, b ) {
    const name1 = a.name.toLowerCase();
    const name2 = b.name.toLowerCase();    
    return (b.default - a.default) || ((name1 > name2) - (name2 > name1))
});

I've got the quick code for string comparison from here: https://stackoverflow.com/a/17765169/2244262

about 4 years ago · Juan Pablo Isaza Relatório
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