I have a datatable in my LWC with sorting that all works well, except when I want to sort on a link it is obviously using the fieldName "nameUrl" to sort with, rather than the label. My column is defined like this
{
label: "Name",
fieldName: "nameUrl",
type: "url",
typeAttributes: { label: { fieldName: "Name" }, target: "_blank" },
sortable: true
},
This is the loading of the grid:
@wire(getAccountsToVerify, { inputdate: "$inputDate" })
accountResult(result) {
this.wiredAccounts = result;
if (result.data) {
this.accounts = result.data.map((record) =>
Object.assign({ nameUrl: "/" + record.Id }, record)
);
this.error = undefined;
} else if (result.error) {
this.error = error;
this.accounts = undefined;
}
}
And my sort code:
updateSort(event) {
let fieldName = event.detail.fieldName;
let sortDirection = event.detail.sortDirection;
this.sortBy = fieldName;
this.sortDir = sortDirection;
let reverse = sortDirection !== "asc";
let data_clone = JSON.parse(JSON.stringify(this.accounts));
this.accounts = data_clone.sort(this.sortData(fieldName, reverse));
}
sortData(field, reverse, primer) {
var key = function (x) {
return primer ? primer(x[field]) : x[field];
};
return function (a, b) {
var A = key(a),
B = key(b);
if (A === undefined) A = "";
if (B === undefined) B = "";
return (A < B ? -1 : A > B ? 1 : 0) * [1, -1][+!!reverse];
};
}
The field is the Account Name, which is a PersonAccount so is made up of a person's first, middle and lastname. How can I get it to sort on the Account Name instead of the url?