如何实施';自然排序';在jqGrid中

如何实施';自然排序';在jqGrid中,jqgrid,natural-sort,Jqgrid,Natural Sort,我开始搜索在jqGrid中实现“NaturalSort”,我有如下NaturalSort的Javascript代码: this.naturalSort = function (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

我开始搜索在jqGrid中实现“NaturalSort”,我有如下NaturalSort的Javascript代码:

this.naturalSort = function (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 < yD) return -1;
        else if (xD > yD) return 1;
    // 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)) ? 1 : -1; }
        // 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 -1;
        if (oFxNcL > oFyNcL) return 1;
    }
    return 0;
};
this.naturalSort=函数(a,b){
变量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}/,,
hre=/^0x[0-9a-f]+$/i,
ore=/^0/,,
i=函数{return naturalSort.insensitive&(“”+s).toLowerCase()| |“”+s},
//将所有字符串转换为带空格的字符串
x=i(a)。替换(sre),| |,“”,
y=i(b)。替换(sre),| |,“”,
//区块/标记化
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'),
//数字、十六进制或日期检测
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;
//首先尝试对十六进制代码或日期进行排序
if(yD)
如果(xDyD)返回1;
//通过拆分数字字符串和默认字符串进行自然排序
for(var cLoc=0,numS=Math.max(xN.length,yN.length);cLocoFyNcL)返回1;
}
返回0;
};
我尝试过,但没有找到通过jqGrid使用上述代码的方法。有谁能指导我如何通过使用上述代码或其他方式实现“自然排序”


在此方面的任何帮助都将不胜感激

Tony-jqGrid的创建者添加了新功能来处理自定义排序:

它将在jqGrid即将发布的版本中提供。以下是Tony为处理NaturalSort订单而修改的代码:

...jqGrid({
...
data: mydata,
...
colModel : [ 
...
{"name": "field1",...,"sortfunc": naturalSort, ...},
...
],
...
});
请注意,对于排序函数,您需要传递3个参数,其中3个参数是升序的排序顺序1和降序的排序顺序-1。 在这种情况下,naturalSort函数应该有3个参数。下面是我修改过的代码

/*
 * 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;
}
/*
*Javascript的自然排序算法-版本0.7-根据MIT许可证发布
*作者:Jim Palmer(基于Dave Koelle的分块思想)
*/
函数自然排序(a、b、d){
如果(d==未定义){d=1;}
变量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}/,,
hre=/^0x[0-9a-f]+$/i,
ore=/^0/,,
i=函数{return naturalSort.insensitive&(“”+s).toLowerCase()| |“”+s},
//将所有字符串转换为带空格的字符串
x=i(a)。替换(sre),| |,“”,
y=i(b)。替换(sre),| |,“”,
//区块/标记化
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'),
//数字、十六进制或日期检测
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;
//首先尝试对十六进制代码或日期进行排序
if(yD)
如果(xDyD)返回d;
//通过拆分数字字符串和默认字符串进行自然排序
for(var cLoc=0,numS=Math.max(xN.length,yN.length);cLocoFyNcL)返回d;
}
返回0;
}
有关已记录问题的更多详细信息,请访问此链接:


Tony-jqGrid的创建者添加了新功能来处理自定义排序:

它将在jqGrid即将发布的版本中提供。以下是Tony为处理NaturalSort订单而修改的代码:

...jqGrid({
...
data: mydata,
...
colModel : [ 
...
{"name": "field1",...,"sortfunc": naturalSort, ...},
...
],
...
});
请注意,对于排序函数,您需要传递3个参数,其中3个参数是升序的排序顺序1和降序的排序顺序-1。 在这种情况下,naturalSort函数应该有3个参数。下面是我修改过的代码

/*
 * 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;
}
/*
*Javascript的自然排序算法-版本0.7-根据MIT许可证发布
*作者:Jim Palmer(基于Dave Koelle的分块思想)
*/
函数自然排序(a、b、d){
如果(d==未定义){d=1;}
变量re=/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$^0x[0-9a-f]+$[0-9]+)/gi,
sre=/(^[]*|[]*$)/g,
dre=/(^([\w]+,?[\w]