Natural Sorting

Natural Sorting

AlanAbsentAlanAbsent Posts: 5Questions: 0Answers: 0
edited June 2011 in Plug-ins
Hello, just wondeirng if anyone can help me out ive been trying to get the natural sorting to work to sort numbers in a row.. but some reaosn the number do not sort right at all say i have 6 7 23 53 when i sort desc it goes in the order of 7 6 23 53, is i ASC it goes in the order of 23 53 7 6 below is this script i am using. any help would be greatly
apperciated

Thanks
[code]

/*
* Natural Sort algorithm for Javascript - Version 0.6 - Released under MIT license
* Author: Jim Palmer (based on chunking idea from Dave Koelle)
* Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
*/
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/,
// convert all to strings and trim()
x = a.toString().replace(sre, '') || '',
y = b.toString().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;
// 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;
}
$(document).ready(function() {
$('#theTable').dataTable( {
"sPaginationType": "full_numbers",
"aoColumnsDefs": [{ "sType": "natural" },
{ "sType": "natural" },
{ "sType": "natural" },
null
]
} );
} );

jQuery.fn.dataTableExt.oSort['string-asc'] = function(a,b) {
return naturalSort(a,b);
};

jQuery.fn.dataTableExt.oSort['string-desc'] = function(a,b) {
return naturalSort(a,b) * -1;
};

[/code]

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    You've redefined the 'string' sorting method that DataTables uses to do natural sorting, which is fine, but you've also set the three first columns to use 'natural' sorting, for which there is no plug-in. I'd be surprised if this isn't giving a Javascript error, unless you have jQuery.fn.dataTableExt.oSort['natural-asc'] (and desc) somewhere else? Was there a reason for doing it that way?

    Allan
  • AlanAbsentAlanAbsent Posts: 5Questions: 0Answers: 0
    No i wasnt getting a error, and really i wasnt sure how todo obtain the process i needed sounds like i need to specify which column i wana use natural sort on which would be the 5th column...

    ive tired this code
    [code]
    $(document).ready(function() {
    $('#theTable').dataTable( {
    "sPaginationType": "full_numbers",
    "aoColumnsDefs": [ null,
    null, null, null,
    { "sType": "natural" }
    ]
    } );
    } );
    [/code]

    and i also tried to change the 'string-desc',etc to 'natural'-desc, asc


    Thanks very much for the help Allan
This discussion has been closed.