0

I'm trying to create a utility class to do all the sorting for lightning datatable. I used the functions that I have on lightning component and change to LWC, like this:

updateSorting(event, fieldName, sortDirection) {
        var fieldName = event.fieldName;
        var sortDirection = event.sortDirection;
        sortedBy = fieldName;
        sortedDirection = sortDirection;
        sortData(fieldName, sortDirection);
    }

    sortData(data, fieldName, sortDirection) {
        var reverse = sortDirection !== 'asc';
        data.sort(this.sortBy(fieldName, reverse));
        return data;
    }

    sortBy(field, reverse, primer) {
        var key = primer ?
            function(x) {return primer(x[field])} :
        function(x) {return x[field]};
        reverse = !reverse ? 1 : -1;
        return function (a, b) {
            return a = key(a)?key(a):'', b = key(b)?key(b):'', reverse * ((a > b) - (b > a));
        }
    }

And insert the sortable on the columns and the call of the methods on the lightning datatable and call this methods like this:

updateSorting(){
      c/UtilityFunctions.updateSorting(event, fieldName, sortDirection);
      }

      sortData(){
      c/UtilityFunctions.sortData(this.lstProposal,fieldName, sortDirection);
      }

      sortBy(){
      c/UtilityFunctions.sortBy(field, reverse, primer);
      }

But it's not working and I don't know why..

1
  • consider importing your utility function as modules (import myFunct from 'c/utilFuncs') make sure you export them accordingly as well
    – glls
    Commented Dec 6, 2021 at 21:37

1 Answer 1

0

Change below code:

return function (a, b) {
    return a = key(a)?key(a):'', b = key(b)?key(b):'', reverse * ((a > b) - (b > a));
}

To :

return function (a, b) {
    a = key(a);
    b = key(b);
    return reverse * ((a > b) - (b > a));
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .