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>