homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
function compareValues(key, order = 'asc') {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string')
? a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string')
? b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
};
}
javascript sort object by value descending
starWarsPeople.map(e=>{
return e
}).sort((a,b)=>{
if (a.height-b.height>1) {return -1;}
else if (a.height-b.height<1) {return 1;}
else {return 0;}
});