Search code examples
jqueryjquery-pluginsjqgrid

How to sort numbers properly in column in format x e y (as power of 10)


I have numbers represented as power of 10, for an example 5e6 (5 000 000), 4e4 (40 000). Is there an elegant way to sort them in jqgrid properly by their values?

I have an unholy idea to calculate values in hidden column and on sort of x e y column perform sort on hidden calculated column, but it seems rather wrong.

Any idea is welcome it may help me to figure out something.


Solution

  • You have two solutions.

    1.You can use sorttype as function. The function accept value and return value. You can consult Guriddo documentation here (look at this parameter in the table) Suppose you have the value in format xey, where the x is the base and y is the exponent, then

    ...
        { name: 'power', width: 100, sorttype : function(a) { var test = a.split("e"); return Math.pow(test[0],test[1]);} },
    ...
    

    2.Your second option is to use a sortfunc to define your custom sort function. Look at the link above on how to use it.

    UPDATE Sorry I missed that your power is of 10, so your code should be changed to: parseFloat(test[0])*(Math.pow(10,test[1]), but I think you got the idea.