1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-02-24 22:54:17 +01:00

Fix: The automatic type detection algorithim can incorrect give precidence to numeric sorting when an empty cell is found. This means that a column of dates with a single empty cell would be sorted as a string. The fix is to skip type detection on empty cells. This also means that the sorting algorithim needs to be updated since sType might not be defined for a column when it is sorted on now (if the data in the column is empty).

Fix: iDataSort, sDataType in the sort function should be declared as local parameters - should give a tiny bit more speed to the sort
This commit is contained in:
Allan Jardine 2011-05-17 21:20:59 +01:00
parent 12bbdad059
commit 5ca44c8405

View File

@ -721,11 +721,11 @@
function ( sData ) function ( sData )
{ {
/* Allow zero length strings as a number */ /* Allow zero length strings as a number */
if ( typeof sData == 'number' || sData === null ) if ( typeof sData == 'number' )
{ {
return 'numeric'; return 'numeric';
} }
else if ( typeof sData != 'string' ) else if ( sData === null || typeof sData != 'string' )
{ {
return null; return null;
} }
@ -762,6 +762,7 @@
} }
} }
console.log('numeric');
return 'numeric'; return 'numeric';
}, },
@ -776,6 +777,7 @@
var iParse = Date.parse(sData); var iParse = Date.parse(sData);
if ( (iParse !== null && !isNaN(iParse)) || (typeof sData == 'string' && sData.length === 0) ) if ( (iParse !== null && !isNaN(iParse)) || (typeof sData == 'string' && sData.length === 0) )
{ {
console.log('date');
return 'date'; return 'date';
} }
return null; return null;
@ -791,6 +793,7 @@
{ {
if ( typeof sData == 'string' && sData.indexOf('<') != -1 && sData.indexOf('>') != -1 ) if ( typeof sData == 'string' && sData.indexOf('<') != -1 && sData.indexOf('>') != -1 )
{ {
console.log('html');
return 'html'; return 'html';
} }
return null; return null;
@ -2682,15 +2685,19 @@
if ( oCol._bAutoType && oCol.sType != 'string' ) if ( oCol._bAutoType && oCol.sType != 'string' )
{ {
/* Attempt to auto detect the type - same as _fnGatherData() */ /* Attempt to auto detect the type - same as _fnGatherData() */
sThisType = _fnDetectType( _fnGetCellData( oSettings, iRow, i, 'type' ) ); var sVarType = _fnGetCellData( oSettings, iRow, i, 'type' );
if ( oCol.sType === null ) if ( sVarType != '' )
{ {
oCol.sType = sThisType; sThisType = _fnDetectType( sVarType );
} if ( oCol.sType === null )
else if ( oCol.sType != sThisType ) {
{ oCol.sType = sThisType;
/* String is always the 'fallback' option */ }
oCol.sType = 'string'; else if ( oCol.sType != sThisType )
{
/* String is always the 'fallback' option */
oCol.sType = 'string';
}
} }
} }
} }
@ -2880,11 +2887,12 @@
nCell = nTds[ (iRow*iColumns) + iColumn ]; nCell = nTds[ (iRow*iColumns) + iColumn ];
/* Type detection */ /* Type detection */
if ( bAutoType ) if ( bAutoType && oSettings.aoColumns[iColumn].sType != 'string' )
{ {
if ( oSettings.aoColumns[iColumn].sType != 'string' ) sValType = _fnGetCellData( oSettings, iRow, iColumn, 'type' );
if ( sValType != '' )
{ {
sThisType = _fnDetectType( _fnGetCellData( oSettings, iRow, iColumn, 'type' ) ); sThisType = _fnDetectType( sValType );
if ( oSettings.aoColumns[iColumn].sType === null ) if ( oSettings.aoColumns[iColumn].sType === null )
{ {
oSettings.aoColumns[iColumn].sType = sThisType; oSettings.aoColumns[iColumn].sType = sThisType;
@ -4598,12 +4606,12 @@
*/ */
var iSortLen = aaSort.length; var iSortLen = aaSort.length;
oSettings.aiDisplayMaster.sort( function ( a, b ) { oSettings.aiDisplayMaster.sort( function ( a, b ) {
var iTest; var iTest, iDataSort, sDataType;
for ( i=0 ; i<iSortLen ; i++ ) for ( i=0 ; i<iSortLen ; i++ )
{ {
iDataSort = aoColumns[ aaSort[i][0] ].iDataSort; iDataSort = aoColumns[ aaSort[i][0] ].iDataSort;
iDataType = aoColumns[ iDataSort ].sType; sDataType = aoColumns[ iDataSort ].sType;
iTest = oSort[ iDataType+"-"+aaSort[i][1] ]( iTest = oSort[ (sDataType?sDataType:'string')+"-"+aaSort[i][1] ](
_fnGetCellData( oSettings, a, iDataSort, 'sort' ), _fnGetCellData( oSettings, a, iDataSort, 'sort' ),
_fnGetCellData( oSettings, b, iDataSort, 'sort' ) _fnGetCellData( oSettings, b, iDataSort, 'sort' )
); );