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

199
Visualizações
sort keeping 2 items on top

I have an array object like

const d = [
    { id: 20, text: 'deaf' },
    { id: 30, text: 'acta', },
    { id: 0, text: 'deema1' },
    { id: -1, text: 'deema2' },
]

I want to sort the array by text but want to keep id -1 and 0 always on top, so the result would be

// sorted
[
    { id: -1, text: 'deema2' },
    { id: 0, text: 'deema1' },
    { id: 30, text: 'acta' },
    { id: 20, text: 'deaf' },
]

I tried to sort like d.sort((a, b) => (a.text).localeCompare(b.text)) but not sure how to handle case -1 and 0

const d = [
    { id: 20, text: 'deaf' },
    { id: 30, text: 'acta', },
    { id: 0, text: 'deema1' },
    { id: -1, text: 'deema2' },
];

d.sort((a, b) => (a.text).localeCompare(b.text))

console.log(d);

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

0

You can work with indexOf:

const d = [
    { id: 20, text: 'deaf' },
    { id: 30, text: 'acta', },
    { id: 0, text: 'deema1' },
    { id: -1, text: 'deema2' },
    { id: 0, text: 'ace' },
    { id: -999, text: 'hello' },
    { id: 800, text: 'game' },
]

d.sort((a, b) => [0,-1].indexOf(b.id) - [0,-1].indexOf(a.id) 
                 || a.text.localeCompare(b.text));

console.log(d);

This will order all objects with id -1 first, and among those, the text property will define the order, then all objects with id 0, (again relatively ordered by text) and finally all other objects by text.

In case you want there to be no distinction between 0 and -1, but sort all those by text among themselves, then use includes instead of indexOf:

const d = [
    { id: 20, text: 'deaf' },
    { id: 30, text: 'acta', },
    { id: 0, text: 'deema1' },
    { id: -1, text: 'deema2' },
    { id: 0, text: 'ace' },
    { id: -999, text: 'hello' },
    { id: 800, text: 'game' },
]

d.sort((a, b) => [0,-1].includes(b.id) - [0,-1].includes(a.id) 
                 || a.text.localeCompare(b.text));

console.log(d);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can put multiple conditions inside of sort to get the desired result. The first condition being more important than the next and so on. This is how it would look like:

const d = [{
    id: 20,
    text: 'deaf'
  },
  {
    id: 30,
    text: 'acta',
  },
  {
    id: 0,
    text: 'deema1'
  },
  {
    id: -1,
    text: 'deema2'
  },
]

const sorted = d.sort((a, b) => {
  if (a.id === -1) {
    return -1;
  }
  if (a.id === 0) {
    return -1;
  }
  return a.text.localeCompare(b.text);
});

console.log(sorted)

about 4 years ago · Juan Pablo Isaza Relatório

0

Slightly modified from one of the answer above to use the pipe operator, this way you can add any other numbers you wish just by using more pipes.

const d = [
    { id: 20, text: 'deaf' },
    { id: 30, text: 'acta', },
    { id: 0, text: 'deema1' },
    { id: -1, text: 'deema2' },
    { id: 0, text: 'ace' },
    { id: -999, text: 'hello' },
    { id: 800, text: 'game' },
]

const sorted = d.sort((a, b) => {
  if (a.id === -1 || a.id === 0) {
    return -1;
  }
  return a.text.localeCompare(b.text);
});

console.log(sorted)
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