1
votes

I'm using ag-grid and I have a small issue with numeric filtering:

enter image description here

As you can see the sorting is going well unless the numbers are 2xxx but it start to be confusing as soon as it reach 3 numbers.

What I do expect is the sorting starting from 500 and go up to 2400+ or the inverse. Maybe I don't know about a special option to add to achieve this, I'm getting crazy trying different options but nothing seems to work. I hope you can help. Thanks.

UPDATE:

I reproduced the issue on the original ag-grid example on filtering: https://plnkr.co/edit/Tn9ZeCH4fmLr2ZHQPivQ?p=preview I just mutated the year values and as you can see it is not sorting as expected.

if (r.year === 2008) r.year = 860
if (r.year === 2012) r.year = 920
1
It seems it is sorting strings instead numbers. Share your code please.Eldar
I can not share a whole project, it is impossible. But I reproduced it here: plnkr.co/edit/Tn9ZeCH4fmLr2ZHQPivQ?p=preview Please try to filter on the year column. This is the example from ag-grid mutated by me.Ayeye Brazo

1 Answers

2
votes

The reason of this is that it sorts data as string, not number. So just add custom sorting into your comparator for your yeat field:

var numberSort = (num1, num2) => {
    return num1 - num2;
  };
var columnDefs = [
    /*The other code is omitted for the brevity*/
    {headerName: "Year", field: "year", width: 90, comparator: numberSort},
    /*The other code is omitted for the brevity*/
];

UPDATE:

It looks like you need to add sortable to gridoptions:

var gridOptions = {
    defaultColDef: {
        filter: true,
        sortable: true
    },
}