ag-Grid String Comparator

I’ve been using ag-Grid and it’s pretty great for representing tabular data. The goal of the ag-Grid team is to create “The Best HTML5 Grid in the World”, and I think they’ve done that. Anyways, just by setting an option in the the grid’s configuration (i.e. gridOptions.enableSorting = true) you can make a grid (a.k.a. table) sorted by a column’s value. But the default sorting on strings is not quite what one would want – at least in many cases. For example, when there’s a space between words in a string or the capitalization is mixed the sorting doesn’t appear to be lexicographical.

Thankfully, the sorting can be customized. I’m using the following method as a comparator.

stringComparator = (value_a, value_b) => {
    return value_a.toLowerCase().localeCompare(value_b.toLowerCase());
}

Leave a Reply

Your email address will not be published. Required fields are marked *