$(document).ready(function() {
$.fn.dataTable.ext.errMode = 'none'; // stop pop-up alerts from datatable errors.
$.fn.dataTable.ext.oSort['nullsort-asc'] = function(a,b) {
console.log("asc val of:" + a + ":val of y:" + b);
//return sortNumbersIgnoreText(a, b, Number.POSITIVE_INFINITY);
const isNumA = !isNaN(a);//check if a is a number
const isNumB = !isNaN(b);//check if b is a number
if(!a && !b){return 0;}//if both are null return 0; don't need to change order
else if(!a){return 1;}//if a is null and b isn't return 1
else if(!b){return -1;} //if b is null and a isn't return -1
else if(isNumA && isNumB){return Number(a) - Number(b);}//if they are both numbers do a number sort
else if(isNumA){return -1;}//if a is a number and b isn't return 1
else if(isNumB){return 1;}//if b is a number and a isn't return -1
else {return (a).localeCompare(b);//neither are number and neither are null, do a standard string sort
}
};
$.fn.dataTable.ext.oSort['nullsort-desc'] = function(a,b) {
if(a == "")
return 1;
if(b == "")
return -1;
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
// return sortNumbersIgnoreText(a, b, Number.NEGATIVE_INFINITY) * -1;
};
$('#example').DataTable({
//"aaSorting": [[3,'asc']],
columnDefs: getColumnDef()
});
} );
/*function sortNumbersIgnoreText(a, b, high) {
//var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;
a = a.replace(/,/g, '');
//a = a.match(reg);
a = a !== null ? parseFloat(a[0]) : high;
b = b.replace(/,/g, '');
//b = b.match(reg);
b = b !== null ? parseFloat(b[0]) : high;
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}*/
function getColumnDef(){
return [
{ type: 'nullsort', targets: 3 },
{ type: 'nullsort', targets: 4}
];
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.