ag-grid sort by column A using hidden column B

Viewed 539

Is there a way to sort by column A (user clicks on A column header) but under the hood use column B?

For example, I have a column "name" that displays a user's name. But then I have a column "name frequency in general population" that is hidden. I want to display the regular "name" but sort by the other column under the hood.

1 Answers

Create your own customer comparator and set it to your name field by using Custom Sorting.

With custom sorting, you have access to all the data in each row where you can choose what should go where.

var columnDefs = [
  { field: 'name', comparator: customComparator },
  { field: 'name frequency in general population' },
];

function customComparator(valueA, valueB, nodeA, nodeB, isInverted) {
  const nodeAValue = nodeA.data['name frequency in general population'];
  const nodeBValue = nodeB.data['name frequency in general population'];
  return (nodeAValue > nodeBValue) ? 1 : -1;
}
Related