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

155
Visualizações
Sort list by two fields doesn't work typescript

I have a list with items in typescript:

let productList = [
   {active: false, location: 2, description: "test 123"},
   {active: false, location: 15, description: "test 123"},
   {active: false, location: 40, description: "test 123"},
   {active: false, location: 15, description: "test 123"},
   {active: false, location: 1, description: "test 123"},
   {active: true, location: 2, description: "test 123"},
   {active: false, location: 2, description: "test 123"},
   {active: false, location: 7, description: "test 123"},
   {active: false, location: 1, description: "test 123"}
 ]

I want to sort this list so I show on top the the items that has active: true and that have same location for example location: 0

I tried this it sorts by active on top:

productList.sort((a, b) => (b.active - a.active));

Now I want to add location too so those with same location are sorted one after other, one of my tries was this but no success:

productList.sort((a, b) => (b.active - a.active)&& ((a.location === b.location) ? 1 : -1) );

My end result I want to have is to show active on top and show immediately after those with same location as active

let productList = [
   {active: true, location: 2, description: "test 123"},
   {active: false, location: 2, description: "test 123"},
   {active: false, location: 2, description: "test 123"},
   {active: false, location: 1, description: "test 123"},
   {active: false, location: 1, description: "test 123"},
   {active: false, location: 7, description: "test 123"},
   {active: false, location: 15, description: "test 123"},
   {active: false, location: 15, description: "test 123"},
   {active: false, location: 40, description: "test 123"},
 ]

Can someone help me please? I'm stuck. I tried several ways but not success.

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

0

You'll need to change your first condition to check if any of the elements that match the current a and b locations are active, not just the currently iterated elements. Here using some().

let productList = [
  { active: false, location: 2, description: "test 123" },
  { active: false, location: 15, description: "test 123" },
  { active: false, location: 40, description: "test 123" },
  { active: false, location: 15, description: "test 123" },
  { active: false, location: 1, description: "test 123" },
  { active: true, location: 2, description: "test 123" },
  { active: false, location: 2, description: "test 123" },
  { active: false, location: 7, description: "test 123" },
  { active: false, location: 1, description: "test 123" }
];

const hasActive = (o) => Number(productList.some(e => o.location === e.location && e.active));

const compare = (a, b) => (
  hasActive(b) - hasActive(a)
  || a.location - b.location
  || b.active - a.active
);

console.log(productList.sort(compare));

A more involved solution which first groups by location, then sorts and maps back to an ungrouped array.

function groupSort(arr) {
  const grouped = {};
  
  for (const object of arr) {
    const { location, active } = object;
    grouped[location] ??= { location, active, objects: [] };

    grouped[location].objects.push(object);
    if (active) {
      grouped[location].active = true;
    }
  }

  return Object.values(grouped)
    .sort((a, b) => b.active - a.active || a.location - b.location)
    .flatMap(({ objects }) => objects.sort((a, b) => b.active - a.active));
};

const productList = [{ active: false, location: 2, description: "test 123" }, { active: false, location: 15, description: "test 123" }, { active: false, location: 40, description: "test 123" }, { active: false, location: 15, description: "test 123" }, { active: false, location: 1, description: "test 123" }, { active: true, location: 2, description: "test 123" }, { active: false, location: 2, description: "test 123" }, { active: false, location: 7, description: "test 123" }, { active: true, location: 1, description: "test 123" }];

const sorted = groupSort(productList);

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