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 (
function sortMultiFields(prop){
return function(a,b){
for(i=0;i b[field1])
return x;
else if(a[field1] < b[field1])
return -x;
}
}
}
How to use (put -(minus) sign before field if you want to sort in descending order particular field)
homes.sort(sortMultiFields(["city","-price"]));
Using above function you can sort any json array with multiple fields. No need to change function body at all