Sorting Issue
Sorting Issue
kraftomatic
Posts: 78Questions: 13Answers: 0
Hey Allan,
I'm currently have a small issue with sorting. I'm not trying to sort currency, but the following list of values don't sort consistently. Here's an example:
104
100.002
101.2
99.8
On columns without a "9X.XX" value, the sorting works great. The smaller number throws it off and it appears that it's sorting via a string rather than a number, but do I need to use one of the added plug-ins to correct this?
Thanks Much.
I'm currently have a small issue with sorting. I'm not trying to sort currency, but the following list of values don't sort consistently. Here's an example:
104
100.002
101.2
99.8
On columns without a "9X.XX" value, the sorting works great. The smaller number throws it off and it appears that it's sorting via a string rather than a number, but do I need to use one of the added plug-ins to correct this?
Thanks Much.
This discussion has been closed.
Replies
Very likely - there must be something in the column that is causing the numeric detection to failover to strings - have you got HTML in the column for example? If so the HTML with numbers plug-in would be what is needed.
Allan
100.097
99.827
99.703
99.629
99.455
100.2
So then I assume I need these two functions:
jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
However, can I tie them in without altering how the table sorts currently? For example, here's what I've got currently:
var table = $('#example').dataTable( {
"aoColumnDefs": [ { "bSortable": false, "aTargets": [0] } ],
"aaSorting": [[ 1, 'asc' ]],
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
} );
Thanks.
Allan
Thanks.
99.3
77.56
101.2
However in some cells, there is an "N/A" value with an href tag around it. Adding the html sort seems to be throwing this off in some columns, but not others. I'm not yet sure how to build a work around for this situation ...
Allan
[code]
Query.fn.dataTableExt.aTypes.unshift( function ( sData )
{
sData = typeof sData.replace == 'function' ?
sData.replace( /<.*?>/g, "" ) : sData;
sData = $.trim(sData);
var sValidFirstChars = "0123456789-";
var sValidChars = "0123456789.";
var Char;
var Char2;
var bDecimal = false;
/* Check for a valid first char (no period and allow negatives) */
Char = sData.charAt(0);
Char2 = sData;
if (Char2 == "N/A")
{
//alert("Char2: " + Char2 + " Char: " + Char);
Char = "0";
Char2 = "0";
//alert("Char2: " + Char2 + " Char: " + Char);
}
if (sValidFirstChars.indexOf(Char) == -1)
{
return null;
}
/* Check all the other characters are valid */
for ( var i=1 ; i