New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'NaturalSort' implementation #430

Closed
garora opened this Issue Feb 22, 2013 · 27 comments

Comments

Projects
None yet
4 participants
@garora

garora commented Feb 22, 2013

My purpose to do not log any issue here. It could be a new feature of jqGrid. I suggest jqGrid is maturing day-by-day so, there should be some facilities so, anyone can add customtype for example if I want to apply custom sort and that is not in-build then there should be some way to tell jqGrid that this is my new type and please sort my data according to this type.

I raised an issue on jqGridForum and StackOverflow but till date there is nothing as a solution.

So, I decided that this is good to take this all in your knowledge.

Can you please see this for more details : http://stackoverflow.com/questions/14865567/how-to-implement-naturalsort-in-jqgrid ?

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 11, 2013

Is anyone taking care of it?

garora commented Aug 11, 2013

Is anyone taking care of it?

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Aug 11, 2013

Owner

Hello,

Can you please look in the docs for sorttype colModel property. It can be a function (If I understand right the problem)

Owner

tonytomov commented Aug 11, 2013

Hello,

Can you please look in the docs for sorttype colModel property. It can be a function (If I understand right the problem)

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 11, 2013

Thanks
It is not working for stringsort
Per description I posted same query on SO
On 11 Aug 2013 21:19, "Tony Tomov" notifications@github.com wrote:

Hello,

Can you please look in the docs for sorttype colModel property. It can be
a function (If I understand right the problem)


Reply to this email directly or view it on GitHubhttps://github.com//issues/430#issuecomment-22459782
.

garora commented Aug 11, 2013

Thanks
It is not working for stringsort
Per description I posted same query on SO
On 11 Aug 2013 21:19, "Tony Tomov" notifications@github.com wrote:

Hello,

Can you please look in the docs for sorttype colModel property. It can be
a function (If I understand right the problem)


Reply to this email directly or view it on GitHubhttps://github.com//issues/430#issuecomment-22459782
.

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Aug 11, 2013

Owner

Simple test case would be fine to see what is happen

Owner

tonytomov commented Aug 11, 2013

Simple test case would be fine to see what is happen

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 11, 2013

Thanks @tonytomov for your prompt reply.
I will upload what I am doing and what are the issues. I would prefer if one added NaturalSorting as a sort type :)

garora commented Aug 11, 2013

Thanks @tonytomov for your prompt reply.
I will upload what I am doing and what are the issues. I would prefer if one added NaturalSorting as a sort type :)

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 18, 2013

Column is set to sort the expected result is 'Natural Sort Order' but the actual result always false:

Following is the snippet used to call custom-sorttype (col:Natural Sort Order):

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames:['Inv No','Date', 'Natural Sort order', 'Amount','Tax','Total','String Sort'],
colModel:[ {name:'id',index:'id', width:60, sorttype:"int"},
{name:'invdate',index:'invdate', width:90, sorttype:"date"},
{name:'name',index:'name', width:100, sorttype:function(cell){return naturalSort(cell,cell);}},
{name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
{name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},
{name:'total',index:'total', width:80,align:"right",sorttype:"float"},
{name:'note',index:'note', width:150, sortable:"string"} ],

    multiselect: true, 
    caption: "NaturalSortOrder using jqrid" });

    var mydata = [
    {id:"1",invdate:"2013-10-01",name:"atest",note:"atest",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"2",invdate:"2013-10-02",name:"Atest",note:"Atest",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"3",invdate:"2013-09-01",name:"test2",note:"test2",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"4",invdate:"2013-10-04",name:"test",note:"test",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"5",invdate:"2013-10-05",name:"test2A",note:"test2A",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"6",invdate:"2013-09-06",name:"test2a",note:"test2a",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"7",invdate:"2013-10-04",name:"Test",note:"Test",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"8",invdate:"2013-10-03",name:"2",note:"2",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"9",invdate:"2013-09-01",name:"1",note:"1",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"10",invdate:"2013-09-01",name:"test34",note:"test34",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"9",invdate:"2013-09-01",name:"1test",note:"1test",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"11",invdate:"2013-09-01",name:"10",note:"10",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"12",invdate:"2013-09-01",name:"test33",note:"test33",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"13",invdate:"2013-09-01",name:"Test1",note:"Test1",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"14",invdate:"2013-09-01",name:"BTest",note:"BTest",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"15",invdate:"2013-09-01",name:"3",note:"3",amount:"400.00",tax:"30.00",total:"430.00"}];

    for(var i=0;i&lt;=mydata.length;i++) 
    jQuery("#list4").jqGrid('addRowData',i+1,mydata[i]);

    //NaturalSort function 
    /*
    * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
    * Author: Jim Palmer (based on chunking idea from Dave Koelle)
    */
    function naturalSort(a, b) {

    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
    sre = /(^[ ]*|[ ]*$)/g,
    dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
    hre = /^0x[0-9a-f]+$/i,
    ore = /^0/,
    i = function (s) { return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s },
    // convert all to strings strip whitespace
    x = i(a).replace(sre, '') || '',
    y = i(b).replace(sre, '') || '',
    // chunk/tokenize
    xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
    yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
    // numeric, hex or date detection
    xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
    yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
    oFxNcL, oFyNcL;
    // first try and sort Hex codes or Dates
    if (yD)
    if (xD &lt; yD) return -1;
    else if (xD &gt; yD) return 1;
    // natural sorting through split numeric strings and default strings
    for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc &lt; numS; cLoc++) {
    // find floats not starting with '0', string or 0 if not defined (Clint Priest)
    oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
    oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
    // handle numeric vs string comparison - number &lt; string - (Kyle Adams)
    if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? 1 : -1; }
    // rely on string comparison if different types - i.e. '02' &lt; 2 != '02' &lt; '2'
    else if (typeof oFxNcL !== typeof oFyNcL) {
    oFxNcL += '';
    oFyNcL += '';
    }
    if (oFxNcL &lt; oFyNcL) return -1;
    if (oFxNcL &gt; oFyNcL) return 1;
    }
    return 0;
    };

});

</script>

garora commented Aug 18, 2013

Column is set to sort the expected result is 'Natural Sort Order' but the actual result always false:

Following is the snippet used to call custom-sorttype (col:Natural Sort Order):

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames:['Inv No','Date', 'Natural Sort order', 'Amount','Tax','Total','String Sort'],
colModel:[ {name:'id',index:'id', width:60, sorttype:"int"},
{name:'invdate',index:'invdate', width:90, sorttype:"date"},
{name:'name',index:'name', width:100, sorttype:function(cell){return naturalSort(cell,cell);}},
{name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
{name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},
{name:'total',index:'total', width:80,align:"right",sorttype:"float"},
{name:'note',index:'note', width:150, sortable:"string"} ],

    multiselect: true, 
    caption: "NaturalSortOrder using jqrid" });

    var mydata = [
    {id:"1",invdate:"2013-10-01",name:"atest",note:"atest",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"2",invdate:"2013-10-02",name:"Atest",note:"Atest",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"3",invdate:"2013-09-01",name:"test2",note:"test2",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"4",invdate:"2013-10-04",name:"test",note:"test",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"5",invdate:"2013-10-05",name:"test2A",note:"test2A",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"6",invdate:"2013-09-06",name:"test2a",note:"test2a",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"7",invdate:"2013-10-04",name:"Test",note:"Test",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"8",invdate:"2013-10-03",name:"2",note:"2",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"9",invdate:"2013-09-01",name:"1",note:"1",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"10",invdate:"2013-09-01",name:"test34",note:"test34",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"9",invdate:"2013-09-01",name:"1test",note:"1test",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"11",invdate:"2013-09-01",name:"10",note:"10",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"12",invdate:"2013-09-01",name:"test33",note:"test33",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"13",invdate:"2013-09-01",name:"Test1",note:"Test1",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"14",invdate:"2013-09-01",name:"BTest",note:"BTest",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"15",invdate:"2013-09-01",name:"3",note:"3",amount:"400.00",tax:"30.00",total:"430.00"}];

    for(var i=0;i&lt;=mydata.length;i++) 
    jQuery("#list4").jqGrid('addRowData',i+1,mydata[i]);

    //NaturalSort function 
    /*
    * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
    * Author: Jim Palmer (based on chunking idea from Dave Koelle)
    */
    function naturalSort(a, b) {

    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
    sre = /(^[ ]*|[ ]*$)/g,
    dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
    hre = /^0x[0-9a-f]+$/i,
    ore = /^0/,
    i = function (s) { return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s },
    // convert all to strings strip whitespace
    x = i(a).replace(sre, '') || '',
    y = i(b).replace(sre, '') || '',
    // chunk/tokenize
    xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
    yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
    // numeric, hex or date detection
    xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
    yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
    oFxNcL, oFyNcL;
    // first try and sort Hex codes or Dates
    if (yD)
    if (xD &lt; yD) return -1;
    else if (xD &gt; yD) return 1;
    // natural sorting through split numeric strings and default strings
    for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc &lt; numS; cLoc++) {
    // find floats not starting with '0', string or 0 if not defined (Clint Priest)
    oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
    oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
    // handle numeric vs string comparison - number &lt; string - (Kyle Adams)
    if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? 1 : -1; }
    // rely on string comparison if different types - i.e. '02' &lt; 2 != '02' &lt; '2'
    else if (typeof oFxNcL !== typeof oFyNcL) {
    oFxNcL += '';
    oFyNcL += '';
    }
    if (oFxNcL &lt; oFyNcL) return -1;
    if (oFxNcL &gt; oFyNcL) return 1;
    }
    return 0;
    };

});

</script>

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora commented Aug 21, 2013

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 24, 2013

@tonytomov - did you get a chance to look into the said issue?

garora commented Aug 24, 2013

@tonytomov - did you get a chance to look into the said issue?

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Aug 24, 2013

Owner

Hello,

Right now I understand what you mean. Using sorttype is not correct way to do natural sort.
Natural sort is a sort algorithm, while sorttype determines the type of the value and convert it to number, sting and etc.

In order to make this to work we need to embed the function in jqGrid or make a possibility to define your own sorting algorithm in jqGrid
I think the second is better. Will see what I can do, but I can begin to work on this after a week.

Regards

Owner

tonytomov commented Aug 24, 2013

Hello,

Right now I understand what you mean. Using sorttype is not correct way to do natural sort.
Natural sort is a sort algorithm, while sorttype determines the type of the value and convert it to number, sting and etc.

In order to make this to work we need to embed the function in jqGrid or make a possibility to define your own sorting algorithm in jqGrid
I think the second is better. Will see what I can do, but I can begin to work on this after a week.

Regards

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Aug 24, 2013

Thanks a lot @tonytomov - I am really excited to get this feature in jqGrid and will be till next week to hear good news for the same :)

Thanks & Regards

garora commented Aug 24, 2013

Thanks a lot @tonytomov - I am really excited to get this feature in jqGrid and will be till next week to hear good news for the same :)

Thanks & Regards

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Sep 4, 2013

@tonytomov we are eagerly awaiting for new feature

garora commented Sep 4, 2013

@tonytomov we are eagerly awaiting for new feature

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Sep 14, 2013

Hi Tony

While you implementing changes to tell jquery own custom sort algorythim is it possible that u can also implement same functionality for folters

Thanks
Gaurav

garora commented Sep 14, 2013

Hi Tony

While you implementing changes to tell jquery own custom sort algorythim is it possible that u can also implement same functionality for folters

Thanks
Gaurav

@mdemori

This comment has been minimized.

Show comment
Hide comment
@mdemori

mdemori Jan 6, 2014

hi sorry but is there any news about this feature? i need to sort in natural order 1, 2, ..., 10, 11 and not 1,10,11, 2, 3 etc. thank you very much!

mdemori commented Jan 6, 2014

hi sorry but is there any news about this feature? i need to sort in natural order 1, 2, ..., 10, 11 and not 1,10,11, 2, 3 etc. thank you very much!

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 7, 2014

@tonytomov - are you releasing this feature? Many of us waiting for this feature

Thanks & Regards,
Gaurav Arora

garora commented Feb 7, 2014

@tonytomov - are you releasing this feature? Many of us waiting for this feature

Thanks & Regards,
Gaurav Arora

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Feb 8, 2014

Owner

Hello,

Just added the possibility to set a custom sort function in colModel.
6131d8a
This include a naturalSort. The implementation is easy - just add a sortfunc in colModel for the field you want to be sorted.

...jqGrid({
...
data: mydata,
...
colModel : [ 
...
{"name": "field1",...,"sortfunc": naturalSort, ...},
...
],
...
});

Note that to the sort function you will need to pass 3 paramaters, where the 3 parameters is the sort order 1 for ascending and -1 for descending.
In this case your naturalSort function should have 3 parameters. Below is the modified code from me.

/*
 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
 */
 function naturalSort (a, b, d) {
    if(d===undefined) { d=1; }
    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
        sre = /(^[ ]*|[ ]*$)/g,
        dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
        hre = /^0x[0-9a-f]+$/i,
        ore = /^0/,
        i = function(s) { return naturalSort.insensitive && (''+s).toLowerCase() || ''+s },
        // convert all to strings strip whitespace
        x = i(a).replace(sre, '') || '',
        y = i(b).replace(sre, '') || '',
        // chunk/tokenize
        xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
        yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
        // numeric, hex or date detection
        xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
        yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
        oFxNcL, oFyNcL;
    // first try and sort Hex codes or Dates
    if (yD)
        if ( xD < yD ) return -d;
        else if ( xD > yD ) return d;
    // natural sorting through split numeric strings and default strings
    for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
        // find floats not starting with '0', string or 0 if not defined (Clint Priest)
        oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
        oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
        // handle numeric vs string comparison - number < string - (Kyle Adams)
        if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? d : -d; }
        // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
        else if (typeof oFxNcL !== typeof oFyNcL) {
            oFxNcL += '';
            oFyNcL += '';
        }
        if (oFxNcL < oFyNcL) return -d;
        if (oFxNcL > oFyNcL) return d;
    }
    return 0;
}

I hope this helps and is more flexible solution.
Enjoy

P.S. When using local data the wrong way is to use addRowData. In this case there is no sorting.
The natural way is to use the data paramater and add the array to it as in the exmple

Owner

tonytomov commented Feb 8, 2014

Hello,

Just added the possibility to set a custom sort function in colModel.
6131d8a
This include a naturalSort. The implementation is easy - just add a sortfunc in colModel for the field you want to be sorted.

...jqGrid({
...
data: mydata,
...
colModel : [ 
...
{"name": "field1",...,"sortfunc": naturalSort, ...},
...
],
...
});

Note that to the sort function you will need to pass 3 paramaters, where the 3 parameters is the sort order 1 for ascending and -1 for descending.
In this case your naturalSort function should have 3 parameters. Below is the modified code from me.

/*
 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
 */
 function naturalSort (a, b, d) {
    if(d===undefined) { d=1; }
    var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
        sre = /(^[ ]*|[ ]*$)/g,
        dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
        hre = /^0x[0-9a-f]+$/i,
        ore = /^0/,
        i = function(s) { return naturalSort.insensitive && (''+s).toLowerCase() || ''+s },
        // convert all to strings strip whitespace
        x = i(a).replace(sre, '') || '',
        y = i(b).replace(sre, '') || '',
        // chunk/tokenize
        xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
        yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
        // numeric, hex or date detection
        xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
        yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
        oFxNcL, oFyNcL;
    // first try and sort Hex codes or Dates
    if (yD)
        if ( xD < yD ) return -d;
        else if ( xD > yD ) return d;
    // natural sorting through split numeric strings and default strings
    for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
        // find floats not starting with '0', string or 0 if not defined (Clint Priest)
        oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
        oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
        // handle numeric vs string comparison - number < string - (Kyle Adams)
        if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? d : -d; }
        // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
        else if (typeof oFxNcL !== typeof oFyNcL) {
            oFxNcL += '';
            oFyNcL += '';
        }
        if (oFxNcL < oFyNcL) return -d;
        if (oFxNcL > oFyNcL) return d;
    }
    return 0;
}

I hope this helps and is more flexible solution.
Enjoy

P.S. When using local data the wrong way is to use addRowData. In this case there is no sorting.
The natural way is to use the data paramater and add the array to it as in the exmple

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 10, 2014

@tonytomov - thanks a ton! Is this version of JQGrid released?
It works for me, I have some issues, I believe these can be handled within my jquery code.
I am closing this issue :)

garora commented Feb 10, 2014

@tonytomov - thanks a ton! Is this version of JQGrid released?
It works for me, I have some issues, I believe these can be handled within my jquery code.
I am closing this issue :)

@garora garora closed this Feb 10, 2014

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Feb 10, 2014

Owner

Hello,
Glad to hear that it works. It works for me too.
The version will be released at the end of week

Regards

Owner

tonytomov commented Feb 10, 2014

Hello,
Glad to hear that it works. It works for me too.
The version will be released at the end of week

Regards

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 10, 2014

@tonytomov - thanks a lot, unfortunately this is not working for some of my colleagues, following is the source code they tried.

This code does not work, even by adding new feature in the source file of jqGrid:

jQuery(document).ready(function () {
jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames: ['Inv No', 'Date', 'Natural Sort order', 'Amount', 'Tax', 'Total', 'String Sort'],
colModel: [{ name: 'id', index: 'id', width: 60, sorttype: "int" },
{ name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
{ name: 'name', index: 'NaturalSort', width: 150, sortfunc: 'naturalSort' },
{ name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
{ name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
{ name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
{ name: 'note', index: 'note', width: 150, sortable: "string" }],

            multiselect: true,
            caption: "NaturalSortOrder using jqrid"
        });

        var mydata = [
        { id: "1", invdate: "2013-10-01", name: "atest", note: "atest", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "2", invdate: "2013-10-02", name: "Atest", note: "Atest", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "3", invdate: "2013-09-01", name: "test2", note: "test2", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "4", invdate: "2013-10-04", name: "test", note: "test", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "5", invdate: "2013-10-05", name: "test2A", note: "test2A", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "6", invdate: "2013-09-06", name: "test2a", note: "test2a", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "7", invdate: "2013-10-04", name: "Test", note: "Test", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "8", invdate: "2013-10-03", name: "2", note: "2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "9", invdate: "2013-09-01", name: "1", note: "1", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "10", invdate: "2013-09-01", name: "test34", note: "test34", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "9", invdate: "2013-09-01", name: "1test", note: "1test", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "11", invdate: "2013-09-01", name: "10", note: "10", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "12", invdate: "2013-09-01", name: "test33", note: "test33", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "13", invdate: "2013-09-01", name: "Test1", note: "Test1", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "14", invdate: "2013-09-01", name: "BTest", note: "BTest", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "15", invdate: "2013-09-01", name: "3", note: "3", amount: "400.00", tax: "30.00", total: "430.00" }];


        for (var i = 0; i <= mydata.length; i++)
            jQuery("#list4").jqGrid('addRowData', i + 1, mydata[i]);

    });

garora commented Feb 10, 2014

@tonytomov - thanks a lot, unfortunately this is not working for some of my colleagues, following is the source code they tried.

This code does not work, even by adding new feature in the source file of jqGrid:

jQuery(document).ready(function () {
jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames: ['Inv No', 'Date', 'Natural Sort order', 'Amount', 'Tax', 'Total', 'String Sort'],
colModel: [{ name: 'id', index: 'id', width: 60, sorttype: "int" },
{ name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
{ name: 'name', index: 'NaturalSort', width: 150, sortfunc: 'naturalSort' },
{ name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
{ name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
{ name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
{ name: 'note', index: 'note', width: 150, sortable: "string" }],

            multiselect: true,
            caption: "NaturalSortOrder using jqrid"
        });

        var mydata = [
        { id: "1", invdate: "2013-10-01", name: "atest", note: "atest", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "2", invdate: "2013-10-02", name: "Atest", note: "Atest", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "3", invdate: "2013-09-01", name: "test2", note: "test2", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "4", invdate: "2013-10-04", name: "test", note: "test", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "5", invdate: "2013-10-05", name: "test2A", note: "test2A", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "6", invdate: "2013-09-06", name: "test2a", note: "test2a", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "7", invdate: "2013-10-04", name: "Test", note: "Test", amount: "200.00", tax: "10.00", total: "210.00" },
        { id: "8", invdate: "2013-10-03", name: "2", note: "2", amount: "300.00", tax: "20.00", total: "320.00" },
        { id: "9", invdate: "2013-09-01", name: "1", note: "1", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "10", invdate: "2013-09-01", name: "test34", note: "test34", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "9", invdate: "2013-09-01", name: "1test", note: "1test", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "11", invdate: "2013-09-01", name: "10", note: "10", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "12", invdate: "2013-09-01", name: "test33", note: "test33", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "13", invdate: "2013-09-01", name: "Test1", note: "Test1", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "14", invdate: "2013-09-01", name: "BTest", note: "BTest", amount: "400.00", tax: "30.00", total: "430.00" },
        { id: "15", invdate: "2013-09-01", name: "3", note: "3", amount: "400.00", tax: "30.00", total: "430.00" }];


        for (var i = 0; i <= mydata.length; i++)
            jQuery("#list4").jqGrid('addRowData', i + 1, mydata[i]);

    });
@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Feb 11, 2014

Owner

Hello,

The problem is in this line of code:

{ name: 'name', index: 'NaturalSort', width: 150, sortfunc: 'naturalSort' },

Why do you change the index name. This is wrong. Set it as it is like this:

{ name: 'name', index: 'name', width: 150, sortfunc: 'naturalSort' },
Owner

tonytomov commented Feb 11, 2014

Hello,

The problem is in this line of code:

{ name: 'name', index: 'NaturalSort', width: 150, sortfunc: 'naturalSort' },

Why do you change the index name. This is wrong. Set it as it is like this:

{ name: 'name', index: 'name', width: 150, sortfunc: 'naturalSort' },
@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 11, 2014

@tonytomov - thanks, it resolved. copy/paste problem

garora commented Feb 11, 2014

@tonytomov - thanks, it resolved. copy/paste problem

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 19, 2014

Is new version of jqGrid released? I did not find newer version in nuget. What is the newer/latest version of jqgrid, which contain new features and bug fixes.

garora commented Feb 19, 2014

Is new version of jqGrid released? I did not find newer version in nuget. What is the newer/latest version of jqgrid, which contain new features and bug fixes.

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 21, 2014

@tonytomov - could you please mind to review following code, it is not working with latest release 4.6.0, if there is any issue of the code please correct:
I attached the screenshot, see the output of normal string sort and naturalsort are same which is wrong:

jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames: ['Inv No', 'Date', 'Natural Sort order', 'Amount', 'Tax', 'Total', 'String Sort'],
colModel: [{ name: 'id', index: 'id', width: 60, sorttype: "int" },
{ name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
{ name: 'name', index: 'name', width: 150, sortfunc: 'naturalSort' },
{ name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
{ name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
{ name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
{ name: 'note', index: 'note', width: 150, sortable: "string" }],

            multiselect: true,
            caption: "NaturalSortOrder using jqrid"
        });

stringsort
naturalsortorder

garora commented Feb 21, 2014

@tonytomov - could you please mind to review following code, it is not working with latest release 4.6.0, if there is any issue of the code please correct:
I attached the screenshot, see the output of normal string sort and naturalsort are same which is wrong:

jQuery("#list4").jqGrid({
datatype: "local",
height: 250,
colNames: ['Inv No', 'Date', 'Natural Sort order', 'Amount', 'Tax', 'Total', 'String Sort'],
colModel: [{ name: 'id', index: 'id', width: 60, sorttype: "int" },
{ name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
{ name: 'name', index: 'name', width: 150, sortfunc: 'naturalSort' },
{ name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
{ name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" },
{ name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" },
{ name: 'note', index: 'note', width: 150, sortable: "string" }],

            multiselect: true,
            caption: "NaturalSortOrder using jqrid"
        });

stringsort
naturalsortorder

@OlegKi

This comment has been minimized.

Show comment
Hide comment
@OlegKi

OlegKi Feb 21, 2014

Contributor

I suppose that use defined naturalSort as a function somewhere in your code. Why you use string value sortfunc: 'naturalSort'? I think you should use sortfunc: naturalSort instead.

Regards
Oleg

Contributor

OlegKi commented Feb 21, 2014

I suppose that use defined naturalSort as a function somewhere in your code. Why you use string value sortfunc: 'naturalSort'? I think you should use sortfunc: naturalSort instead.

Regards
Oleg

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 21, 2014

Hi Oleg,

This function is modified above by Tony.

Unfortunately it is not working in any form.

Thanks for taking look.
On 21 Feb 2014 15:45, "Dr. Oleg Kiriljuk" notifications@github.com wrote:

I suppose that use defined naturalSort as a function somewhere in your
code. Why you use string value sortfunc: 'naturalSort'? I think you
should use sortfunc: naturalSort instead.

Regards
Oleg

Reply to this email directly or view it on GitHubhttps://github.com//issues/430#issuecomment-35715586
.

garora commented Feb 21, 2014

Hi Oleg,

This function is modified above by Tony.

Unfortunately it is not working in any form.

Thanks for taking look.
On 21 Feb 2014 15:45, "Dr. Oleg Kiriljuk" notifications@github.com wrote:

I suppose that use defined naturalSort as a function somewhere in your
code. Why you use string value sortfunc: 'naturalSort'? I think you
should use sortfunc: naturalSort instead.

Regards
Oleg

Reply to this email directly or view it on GitHubhttps://github.com//issues/430#issuecomment-35715586
.

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 21, 2014

Hi Oleg,

I must say "To err is human!"
My bad, a silly mistake in jquery function naturalSort missed semi-colon (;) and corrected calling syntax as you suggested:

{ name: 'name', index: 'name', width: 150, sortfunc: naturalSort },

and all is set, good to go. I got the expected results. Thanks to make my life easier 👍

naturalstringsort-corrected

garora commented Feb 21, 2014

Hi Oleg,

I must say "To err is human!"
My bad, a silly mistake in jquery function naturalSort missed semi-colon (;) and corrected calling syntax as you suggested:

{ name: 'name', index: 'name', width: 150, sortfunc: naturalSort },

and all is set, good to go. I got the expected results. Thanks to make my life easier 👍

naturalstringsort-corrected

@tonytomov

This comment has been minimized.

Show comment
Hide comment
@tonytomov

tonytomov Feb 21, 2014

Owner

Hello,
Please look at my code above. Oleg is right the function should be not passed as string, but as I write it in my original post
If you do this you can very easy test if it works if you put alert just at the beginning of the naturalSort function.

Owner

tonytomov commented Feb 21, 2014

Hello,
Please look at my code above. Oleg is right the function should be not passed as string, but as I write it in my original post
If you do this you can very easy test if it works if you put alert just at the beginning of the naturalSort function.

@garora

This comment has been minimized.

Show comment
Hide comment
@garora

garora Feb 21, 2014

@tonytomov - thanks it works, I replied in above.
Your code made my life easy 💯

garora commented Feb 21, 2014

@tonytomov - thanks it works, I replied in above.
Your code made my life easy 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment