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..