1

In my case, Once I clear the column sorting and try to resort to the Ascending by the slick grid header menu. During this action, the column values are shuffled, again and again. I don't know the reason why sorting is not working properly in the slick grid.

enter image description here

As per our expectation, Using the slick grid header menu sorting (ascending) and repeatedly clicking the sort ascending, the column value should not shuffle again and again. So kindly provide a proper solution for this sorting issue.

Software versions:

  • Angular: 13.3.9

  • Angular-Slickgrid: 4.3.1

  • slickgrid-universal: 1.4.0

  • TypeScript: 4.6.4

  • Operating System: Windows 10

  • Node: 14.18.3

  • NPM: 6.14.8

hari
  • 47
  • 2

1 Answers1

0

Please note that I'm the author of Angular-Slickgrid

That was a bug, so you could have open a GitHub issue for that (I opened one for your issue 1052). I looked into this and it was challenging to fix, the lib uses MomentJS (moment-mini actually) and date sorting with Moment is not exactly straightforward when dealing with potential null values. Anyway long story short, I have a fix in this Slickgrid-Universal PR (to be released in the next few days for Angular-Slickgrid 5.x only).

However please note that I only support latest version and I see you are on older version so you won't get the fix unless you upgrade to latest. It is simply too much work for a single person (me) to support more than 1 version at a time, since the lib is a free Open Source project and I don't get any money from this work (apart from the very rare ko-fi contributions).

What if you can't upgrade just yet? You can define your own sortComparer on each column, so you could add the fix manually but that means that you will have to add the sortComparer on every date field of your grid(s)... so even though it would be easier to upgrade, at least you now have a workaround in the meantime.

this.columnDefinitions = [
{
  id: 'finish', name: 'Finish', field: 'finish', sortable: true,
  // ... other options
  sortComparer: (val1, val2, sortDirection) => {
    let diff = 0;
    if (val1 === val2) {
      diff = 0;
    } else {
      let date1: Moment | Date = moment(val1, 'YYYY-MM-DD', true);
      let date2: Moment | Date = moment(val2, 'YYYY-MM-DD', true);
      // when date is invalid, we'll create a temporary old date
      if (!date1.isValid()) {
        date1 = new Date(1001, 1, 1);
      }
      if (!date2.isValid()) {
        date2 = new Date(1001, 1, 1);
      }
      diff = date1.valueOf() - date2.valueOf();
    }

    return sortDirection * diff;
  },
}]

if you plan to reuse it multiple time then extract it to a separate function & file

function sortDateWithFix(val1, val2, sortDirection) {
  let diff = 0;
  if (val1 === val2) {
    diff = 0;
  } else {
    let date1: Moment | Date = moment(val1, 'YYYY-MM-DD', true);
    let date2: Moment | Date = moment(val2, 'YYYY-MM-DD', true);
    // when date is invalid, we'll create a temporary old date
    if (!date1.isValid()) {
      date1 = new Date(1001, 1, 1);
    }
    if (!date2.isValid()) {
      date2 = new Date(1001, 1, 1);
    }
    diff = date1.valueOf() - date2.valueOf();
  }

  return sortDirection * diff;
}

this.columnDefinitions = [
{
  id: 'start', name: 'Start', field: 'start', sortable: true,
  sortComparer: sortDateWithFix,
},
{
  id: 'finish', name: 'Finish', field: 'finish', sortable: true,
  sortComparer: sortDateWithFix,
}]

Note that the new code is taking slightly more time to execute because we now create a temp date in the past to sort nullish values, however it seems negligeable 1240ms vs 1125ms on the first sort of 50k unsorted rows (so about 100-150ms longer than before).

ghiscoding
  • 12,308
  • 6
  • 69
  • 112