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

396
Visualizações
How to sort Json file based on value of object?

I have below json file named all, and contain below data

[{"name":{"common":"Dominican Republic","official":"Dominican Republic","nativeName":{"spa":{"official":"República Dominicana","common":"República Dominicana"}}},"region":"Americas","area":48671.0,"flags":{"png":"https://flagcdn.com/w320/do.png","svg":"https://flagcdn.com/do.svg"}},{"name":{"common":"Heard Island and McDonald Islands","official":"Heard Island and McDonald Islands","nativeName":{"eng":{"official":"Heard Island and McDonald Islands","common":"Heard Island and McDonald Islands"}}},"region":"Antarctic","area":412.0,"flags":{"png":"https://flagcdn.com/w320/hm.png","svg":"https://flagcdn.com/hm.svg"}},{"name":{"common":"Ghana","official":"Republic of Ghana","nativeName":{"eng":{"official":"Republic of Ghana","common":"Ghana"}}},"region":"Americas","area":238533.0,"flags":{"png":"https://flagcdn.com/w320/gh.png","svg":"https://flagcdn.com/gh.svg"}}]

I want to sort this file based on name.official value, I wrote below codes but it does not work. how can I achieve that?

const path = "../all.json;
getCuntries(path);

async function getCuntries(path) {
    const response = await fetch(path);
    const scrampled = await response.json();
    const parsed = await JSON.parse(JSON.stringify(scrampled));
    const data = parsed.sort(GetSortOrder("name"));
    console.log(data)
}

function GetSortOrder(prop) {
    return function(a, b) {
        if (a[prop][1] > b[prop][1]) {
            return 1;
        } else if (a[prop][1] < b[prop][1]) {
            return -1;
        }
        return 0;
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your comparator method is a bit messed up. First, comparing strings with > and < operators may lead to unexpected behaviour sometimes and this is not the recommended way to compare strings. And second (which is probably the root cause), you should access object values with their keys and not with indexes (like you’re doing with [1]). This should work - make it the body of function (a, b):

const aPassedProp = a[prop];
const bPassedProp = b[prop];
return aPassedProp.official.localeCompare(bPassedProp.official);

The localeCompare method can compare your strings in an appropriate way: you can learn more here.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't access an object key by index. As it's is not specified if the object key should be dynamic, here a simple solution where it's not:

const data = [
  { "name": { "official": "Republic of Ghana"} },
  { "name": { "official": "Heard Island and McDonald Islands" } },
  { "name": { "official": "Dominican Republic" } },
]; // shortened for readability

const sortArray = (a, b) => {
  const aCompare = a.name.official;
  const bCompare = b.name.official;
  
  if (aCompare === bCompare) {
    return 0;
  }

  return aCompare < bCompare ? -1 : 1;
};

data.sort(sortArray);

console.log(data);

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