ngOrderObjectBy
ngOrderObjectBy copied to clipboard
Sorting objects where the field may be null for some
Can you include a change to treat null/undefined (e.g. missing sort field in few objects from the array being sorted) as a usual value. Currently in such case, the final sorted output seems random.
A quick fix I tried which worked for my case, was to change the comparison line of code as follows, so that the null values appear first for ascending sort and last for descending sort.
comparator = (reducedA||'') > (reducedB||'') ? 1 : -1;
But maybe there is a better or configurable way to achieve it
I ran into a similar problem with today with:
ng-repeat="item in ctrl.items | orderObjectBy : 'supplier.name' : true"
In some rows 'item.supplier' would not be set yet so would be 'undefined', my solution was to trick the index function to use '-1' as a sting for undefined values.
function index(obj, i) {
if (typeof obj !== 'undefined') {
return obj[i];
} else {
return '-1';
}
}