Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting for numeric column does not work properly - Angular-ui-grid

I've noticed when I sort a column with numeric values it does not seem to sort correctly. For example I have a column with the following numbers: 565, 5786, 6335, 6351, 61447. When I sort the column (DESC) the numbers are ordered as follows:

  • 6351
  • 6335
  • 61447
  • 5786
  • 565

The problem seems to be in sorting numbers vs. text.

For reference I am using "angular-ui-grid": "~3.1.1"

like image 653
Ekta Verma Avatar asked Jul 23 '26 18:07

Ekta Verma


2 Answers

Try giving column type as "number" for this. The sort algorithm is chosen based on the column type. ui-grid will guess the type based on the data, although if you load data asynchronously after the columns it will often decide all your columns are string. You can explicitly set the column type in the column def using type:'number'.

More details here

like image 86
Peter Avatar answered Jul 25 '26 11:07

Peter


Just add a type: 'number' to the column definition -columnDefs- in your js file. it will work well.

like image 42
Noha Shehab Avatar answered Jul 25 '26 10:07

Noha Shehab