From this original question, how would I apply a sort on multiple fields?
Using this slightly adapted structure, how would I sort city (ascending) & then price (descending)?
var homes = [
{"h_id":"3",
"city":"Dallas",
"state":"TX",
"zip":"75201",
"price":"162500"},
{"h_id":"4",
"city":"Bevery Hills",
"state":"CA",
"zip":"90210",
"price":"319250"},
{"h_id":"6",
"city":"Dallas",
"state":"TX",
"zip":"75000",
"price":"556699"},
{"h_id":"5",
"city":"New York",
"state":"NY",
"zip":"00010",
"price":"962500"}
];
I liked the fact than an answer was given which provided a general approach. Where I plan to use this code, I will have to sort dates as well as other things. The ability to "prime" the object seemed handy, if not a little cumbersome.
I've tried to build this answer into a nice generic example, but I'm not having much luck.
sort(["first-field", "ASC"], ["second-field", "DSC"]);This is further complicated when I try to add in the "primer" logic of the first answer so that I can handle dates, case-insensitivity etc.homes.sort((a, b) =>…)witha.propandb.prop.a.prop - b.propsorts numerically,a.prop.localeCompare(b.prop)lexicographically, and(b.prop < a.prop) - (a.prop < b.prop)generically. To sort descending instead of ascending, negate the return value (e.g.b.prop - a.propinstead ofa.prop - b.prop).