jqGrid - sorting numbers and text
Solution 1
You can use sorttype
defined as function. Inside of the function you can use parseInt
to test whether the value is integer or not. In case of integer value the function can return the integer value. In case of non-integer the function can return original string value. See the answer for the code example.
Solution 2
you can just set sorttype in colModel as
sorttype: 'number'

steve_o
Updated on November 25, 2022Comments
-
steve_o 6 months
Using jqGrid 4.5.2. In my dataset, I have a column that can have either integers or text in it. I have the
sorttype
set totext
.The data that's returned going to be mixed in the grid, and could contain both characters or numbers. It may contain only letters, only numbers or a mixture of both. If I click on the column to sort it in descending order, it goes:
400 350 300 200 1100 1020 1010 1000 100
The numbers can vary, as can the letters. Is there a way to define a custom sorttype function to properly sort a number like a number and a string like a string in the same column? If so, how?
I've found examples where a
CASE
type statement was used, but since the contents of the cells will not be known, I can't do that. Would appreciate any thoughts.EDIT
Based on @Oleg's answer, I've tried a couple different ways to implement the
sorttype
as a function. However, I can't seem to make it fire. It will sort the data, but appears to sort everything as a string. I've got someconsole.log
statements in the code that should dump out whatever values are there, but nothings being shown.First attempt was to have the function inside the
colModel
.{name:"MySource", index:"MySource", width:40, align:"left", sorttype: function (cell, rowObject) { if (typeof cell === "string" && /^test(\d) + $/i.test(cell)) { console.log("inside if custom sort - cell = " + cell ); return parseInt(cell); } else { console.log("else - cell = " + cell ); return cell; } },
Second attempt was after looking at another code example you had on a similar question and creating a function & then calling that function from the
sorttype
.Here is the
colModel
:{name:"MySource", index:"MySource", width:40, align:"left", sorttype: function (cell) { return myCustSort (cell) ; } } }
and the
myCustSort
function:function myCustSort (myCell) { if (typeof myCell === "string" && /^test(\d) + $/i.test(myCell)) { console.log("inside if custom sort - cell = " + myCell ); return parseInt( myCell); } else { console.log("else - cell = " + myCell ); return myCell; } } // end myCustSort
When I click on that column header, I can see the data in the grid sorting, but neither of the displays puts anything in the console log. Shouldn't it fire & do so when the column header is clicked to sort it, or am I missing something?
Thanks!
-
Sai Avinash over 9 years@oleg..can you please give your sugesstion on this..when you have time..i am really worried about my approach to mvc. stackoverflow.com/questions/20514446/…
-
steve_o over 9 years@oleg - I looked at your example, but am having issues trying to make it work. I don't believe it is firing the
sorttype
function. I've edited the question to show what I've attempted. -
Oleg over 9 years@steve_o: You don't posted JavaScript code which creates the grid. Some parts of the code could be very important. You wrote "I can't seem to make it fire." It's important to understand that
sorttype
will be used only if you use client side sorting. Ifsorttype
function not called at all it means that you probably usedatatype: "json"
ordatatype: "json"
and you don't setloadonce: true
. In the case jqGrid just send the sorting column name and the direction of the sorting to the server and the server is responsible for sorting. So you should implement sorting in your server code.