Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda