ngOrderObjectBy icon indicating copy to clipboard operation
ngOrderObjectBy copied to clipboard

Sorting objects where the field may be null for some

Open nitinka01 opened this issue 8 years ago • 1 comments

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

nitinka01 avatar Mar 31 '16 03:03 nitinka01

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';
    }
}

louisl avatar Mar 16 '17 12:03 louisl