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

2

7

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"

Mislike answered 13/4, 2016 at 8:17 Comment(2)
the numbers are sorted as String and not as numbers. look at the example: ui-grid.info/docs/#/tutorial/103_filteringReady
@Ready Thanks :) Yes the problem was due to the datatype of the column.Mislike
C
10

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

Curler answered 13/4, 2016 at 8:36 Comment(2)
I had mentioned the type in columnDef using type='number'. But somehow it was not picking up. I had to explicitly set the column type to 'number' for that particular field and now it works. Thanks a lot for your help :)Mislike
You are wc! In column defs you had to use type:"number", colon instead of equals..Curler
S
2

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

Stringency answered 26/2, 2019 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.