VadimDez / ngx-order-pipe

▼ Angular 5+ orderBy pipe
https://vadimdez.github.io/ngx-order-pipe/
MIT License
243 stars 57 forks source link

Multiple orders with date, second item is reversed #91

Closed bluecaret closed 4 years ago

bluecaret commented 4 years ago

I have a strange issue that is causing my second column that needs to be sorted to be sorted in reverse order from the first.

Stackblitz: https://stackblitz.com/edit/ngx-order-pipe-njfy7p

In the example above it is sorted by DOB and then by Name. But the name column is in reverse order even though both should be ascending. If I sort by multiple columns with anything other than a date it seems to work fine.

Angular v7 ngx-order-pipe v2.0.4

VadimDez commented 4 years ago

The problem is that:

new Date(1978, 10, 12, 0, 0, 0, 0) == new Date(1978, 10, 12, 0, 0, 0, 0)

Will return false. Probably the solution would be to extend comparison functions and use Date's getTime() before comparing two Dates.

For now (before actual fix) you could pass your own comparator that handles Dates https://github.com/VadimDez/ngx-order-pipe#arguments For example;

(a, b) => {
    if (a instanceof Date) {
        a = a.getTime();
        b = b.getTime();
    }
    if (a === b) {
      return 0;
    }
    if (a == null) {
      return 1;
    }
    if (b == null) {
      return -1;
    }
    return a > b ? 1 : -1;
}
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.