I have this object:
[ {'id': 3, 'count': 9}, {'id': 4,'count': null}, {'id':5,'count':4}]
Basically, the order should be count but if count is null, the item(s) should be placed to the bottom of the sort. I tried the following but it is not working. The above should give (for asc):
{'id': 3, 'count': 9}
{'id':5,'count':4}
{'id': 4,'count': null}
When descending:
{'id': 5, 'count': 4}
{'id':3,'count':9}
{'id': 4,'count': null}
Code
let sortValue = -1; //asc
let direction = 'asc';
if (direction != "asc") {
sortValue = 1;
}
objArr.sort(function(a: Item, b: Item) {
const aCount = a['count'] ;
const bCount = b['count'];
if ( aCount == null){
return -1;
}
if ( bCount == null){
return -1;
}
return aCount>bCount? -1 * sortValue : aCount<bCount ? sortValue : 0;