Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!