Below is an example of my object array:
let plans = [
{
surf: 5,
price: 299,
cprice: 199,
cdur: 3,
},
{
surf: 5,
price: 249,
cprice: 199,
cdur: 3,
},
{
surf: 15,
price: 149,
cprice: "",
cdur: "",
},
];
In order to sort this array, I've implemented the following comparisons:
function cpriceDesc( a, b ) {
if ( a.cprice < b.cprice ){
return -1;
}
if ( a.cprice > b.cprice ){
return 1;
}
return 0;
}
function cpriceAsc( a, b ) {
if ( a.cprice > b.cprice ){
return -1;
}
if ( a.cprice < b.cprice ){
return 1;
}
return 0;
}
While sorting works correctly for objects with a cprice value, those without one always end up at the beginning of the sorted array. How can I ensure that they are placed at the end instead?