cellRenderer doesn't actually have anything to do with sorting; as Niall points out, what you need is to modify the column definition to use a custom sorting function. From the example code, let's say you're working with the first example, so your columnDef for athlete looks like this:
var columnDefs = [
{headerName: "Athlete", field: "athlete", width: 150, sort: 'desc'},
...
];
You need to add a comparator to that columnDef:
{headerName: "Athlete", field: "athlete", width: 150, sort: 'desc',
comparator: function(valueA, valueB, nodeA, nodeB, isInverted)
{return caseInsensitiveSort(valueA, valueB, nodeA, nodeB, isInverted);}
}
and then add your function for sorting below.
function caseInsensitiveSort(valueA, valueB, nodeA, nodeB, isInverted) {
return valueA.toLowerCase().localeCompare(valueB.toLowerCase());
}
Note that you pass nodes and isInverted, even though you don't have to use them, because that's the signature that ag-grid expects.
A case where you would use the isInverted flag is if you need to handle certain values like blanks or nulls separately. Here's a more detailed function that sorts those values to the bottom, regardless of direction:
function caseInsensitiveSort(valueA, valueB, nodeA, nodeB, isInverted) {
if( valueA === "" || valueA === null ) {
if( valueB === "" || valueB === null ) {
return 0; // a and b are both blank; 0 means identical
} else {
return (isInverted ? -1 : 1); // a is null, b is not; return 1 for normal sort or -1 for inverted
}
}
if( valueB === "" || valueB === null ) {
return (isInverted ? 1 : -1); // b is null, a is not; return -1 for normal or 1 for inverted to get the opposite result as above
}
return valueA.toLowerCase().localeCompare(valueB.toLowerCase());
};
You could also set that comparator as part of a column type and then apply the column type to the columnDef; this way, you can easily use the same sort function across multiple columns.
var columnDefs = [
{headerName: "Athlete", field: "athlete", width: 150, sort: 'desc', type: 'text'},
...
];
var gridOptions = {
columnDefs: columnDefs,
... // whatever else you set
columnTypes: {
"text": {comparator: function(valueA, valueB, nodeA, nodeB, isInverted)
{return caseInsensitiveSort(valueA, valueB, nodeA, nodeB, isInverted);}
}
};
I have a significantly more complicated example that I've just used on my current project at work; as time permits, I'll narrow it down a bit for a plunkr so you can see how the whole thing works. The ag-grid documentation is pretty useful once you know what you're doing, but the information it has is frequently spread out among several pages, and there aren't many examples that show you exactly what you need to know.
sort: 'desc'
orsort: 'asc'
in addition to the comparator property – Orest