I want to sort a table by the length of the data. But there are some null values coming. This is my sorter function
sorter: (a, b) => a.rechargeType.length - b.rechargeType.length
following error coming when there is a null value
TypeError: Cannot read property 'length' of null
You can simply do that,
sorter: (a, b) => {
if(a && a.rechargeType && a.rechargeType.length && b && b.rechargeType && b.rechargeType.length) {
return a.rechargeType.length - b.rechargeType.length;
} else if(a && a.rechargeType && a.rechargeType.length) {
// That means be has null rechargeType, so a will come first.
return -1;
} else if(b && b.rechargeType && b.rechargeType.length) {
// That means a has null rechargeType so b will come first.
return 1;
}
// Both rechargeType has null value so there will be no order change.
return 0;
}
This way null values will come last. For more reading about the return value of sort compare function you can read this.