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

274
Vistas
Sorting an Array of Objects by multiple criteria

I have an Array of Objects (Properties) that I need to sort by multiple criteria, 4 in total. I have managed two of them, but the second and third are giving me issues.

The sorting order has to be:

1st - if a property has a status of "Active", that goes before a property with a status of "Escrow", this one is easy because I can just so alphabetically since "active" goes before "escrow".

2nd - If a property has a property-type of "Multifamily" that goes before the rest (not working)

3rd - If a property has a status of "Land" that goes before the rest (but after Multifamily, not working)

4th - Sort by number of Units, again, this one is easy.

Here is my code so far, I'm using a microlibrary to sort Arrays but the usuall Array.sort logic functions will work here, the library is called thenBy.js:

Not only the "Multifamily" and "Land" sorting is not working, it's also duplicating entries...I'm really not sure how to approach this one.

const filtered_properties = []

filtered_properties.sort(
  firstBy(function(v1, v2) {
    if (v1.status > v2.status) {
      return 1;
    } else if (v1.status < v2.status) {
      return 0;
    }
  })
  // Not working
  .thenBy(function(v1) {
    return (
      v1["property-type"] &&
      v1["property-type"][0].name === "Multifamily"
    );
  })
  // Not working
  .thenBy(function(v1) {
    return (
      v1["property-type"] &&
      v1["property-type"][0].name === "Land"
    );
  })
  .thenBy(function(v1, v2) {
    return v2.units - v1.units;
  })
);
<script src="https://cdn.jsdelivr.net/npm/thenby@1.3.4/thenBy.min.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Depending on your properties, you could compare with the wanted top string and take the delta of b - a of the comparison and if you need the wanted string at bottom, switch a and b.

The same applies for all other comparisons where an item has to be sorted to top.

array.sort((a, b) =>
    (b.status === 'Active') - (a.status === 'Active') ||
    (b.type === 'Multifamily') - (a.type === 'Multifamily') ||
    (b.status === 'Land') - (a.status === 'Land') ||
    a.units - b.units
);
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