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

120
Visualizações
Sorting an array with a compare function - javascript

I came across this sorting of an array and I do not understand the compare function at the point where '-1 : 1' is included. Please can some one explain to me what the compare function is doing exactly?

const rows = [
  createECG('ad0917d4-e02d-4c46-a5aa-dd83717684e9', '28/06/2022', 'pending'),
  createECG('9034124a-2ac8-4d52-8711-ef1351a5c18a', '18/05/2022', 'complete'),
  createECG('a2a7a1f4-81a0-4b70-b1e2-ee7a384a1f4f', '01/05/2022', 'pending'),
  createECG('e5427e62-5897-4eee-a6a6-8f9925feef96', '21/04/2022', 'complete')
].sort((a) => (a.status === 'pending' ? -1 : 1)); // The object 'a' has a property called 'status'

console.log(rows);

function createECG(id, date, status) {
  return {id, date, status};
}

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

0

The comparison function returns -1 if the first argument a as lower than b if its status is pending. This indicates that a is lower than b, so it should be sorted ahead. The intended result is that all the pending elements will be at the front of the result.

But this isn't really valid, because the comparison function is required to be consistent -- if a is lower than b, then b should not also be lower than a. When it's comparing two pending objects, it will return -1 when they're passed in either order. The comparison function should return 0 in this case, but it doesn't.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to take the delta of the comparison for a symmetrically sorting.

function createECG(id, date, status) {
    return { id, date, status };
}

const
    rows = [
        createECG('ad0917d4-e02d-4c46-a5aa-dd83717684e9', '28/06/2022', 'pending'),
        createECG('9034124a-2ac8-4d52-8711-ef1351a5c18a', '18/05/2022', 'complete'),
        createECG('a2a7a1f4-81a0-4b70-b1e2-ee7a384a1f4f', '01/05/2022', 'pending'),
        createECG('e5427e62-5897-4eee-a6a6-8f9925feef96', '21/04/2022', 'complete')
    ]
    .sort((a, b) => (b.status === 'pending') - (a.status === 'pending'));

console.log(rows);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Sorting is just like filtering, your code means... When a.status === 'pending' equal to 1, means this item has the status property set to pending, and when it equals -1 means the status is not set to pending, then as 1 is correct and superior then Items with pending status will come first

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