Arshath Shameer
Arshath Shameer

Reputation: 453

Sorting works inconsistently in ng-bootstrap table, when table column contains null or empty values (Angular 8)

I'm following official ng bootstrap web site to implement this functionality. - Url

But whenever there is an empty or null value in column, sorting is not working as expected and this happens when you use "Sort Ascending" or "Sort Descending" actions in any column.

And i believe we need to fix a bug from this part in their implementation. Please advise how i need to tweak this

const compare = (v1: string, v2: string) => v1 < v2 ? -1 : v1 > v2 ? 1 : 0;

I did tweak in this way but it is not working as expected.

v1 = v1 == '' ? ' ' : v1; 
v2 = v2 == '' ? ' ' : v2;
if (v1 === v2) { return 0 }; 
if (v1 < v2) { return -1 }; 
if (v1 > v2) { return 1 }; 

Upvotes: 0

Views: 403

Answers (2)

IAfanasov
IAfanasov

Reputation: 4993

This is how javascript works and has nothing to do with ng-bootstrap. A developer needs to decide how to sort empty values (put them at the beginning of the set or at the end) and provide respectful comparison logic.

Upvotes: 0

Arshath Shameer
Arshath Shameer

Reputation: 453

In case if any of you interested here is the solution for this issue.

function compare(v1, v2) {
  if(v1 < v2 || !v1) {
    return -1;
    }

    if(v1 > v2 || !v2) {
      return 1;
    }    
    return 0;
}

Upvotes: 1

Related Questions