From ad1126dcdce2aadbdb3e5b0db57d6232f093e1ee Mon Sep 17 00:00:00 2001 From: Michael Buehler Date: Mon, 21 Dec 2015 15:49:37 +0100 Subject: [PATCH 1/3] FIx - removed html from comparison strings --- sorting/natural.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sorting/natural.js b/sorting/natural.js index 1a26f32..0ce4f2d 100644 --- a/sorting/natural.js +++ b/sorting/natural.js @@ -36,12 +36,15 @@ function naturalSort (a, b) { // convert all to strings and trim() x = a.toString().replace(sre, '') || '', y = b.toString().replace(sre, '') || '', + //strip html from the strings + xH = $(x).text(), + yH = $(y).text(), // 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'), + xN = xH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + yN = yH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), // numeric, hex or date detection - xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)), - yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null; + xD = parseInt(xH.match(hre), 10) || (xN.length !== 1 && xH.match(dre) && Date.parse(xH)), + yD = parseInt(yH.match(hre), 10) || xD && yH.match(dre) && Date.parse(yH) || null; // first try and sort Hex codes or Dates if (yD) { From a73f4bb70b332ec36f6502a7263ca7664dfd6aad Mon Sep 17 00:00:00 2001 From: Michael Buehler Date: Mon, 4 Jan 2016 14:12:39 +0100 Subject: [PATCH 2/3] Use regex for html stripping. Let users choose if they want to strip html or not. --- sorting/natural.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/sorting/natural.js b/sorting/natural.js index 0ce4f2d..efd6305 100644 --- a/sorting/natural.js +++ b/sorting/natural.js @@ -17,6 +17,15 @@ * { type: 'natural', targets: 0 } * ] * } ); + * + * Html can be stripped from sorting by using 'natural-nohtml' such as + * + * $('#example').dataTable( { + * columnDefs: [ + * { type: 'natural-nohtml', targets: 0 } + * ] + * } ); + * */ (function() { @@ -27,24 +36,27 @@ * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js */ -function naturalSort (a, b) { +function naturalSort (a, b, html) { 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/, + htmre = /(<([^>]+)>)/ig, // convert all to strings and trim() x = a.toString().replace(sre, '') || '', - y = b.toString().replace(sre, '') || '', - //strip html from the strings - xH = $(x).text(), - yH = $(y).text(), + y = b.toString().replace(sre, '') || ''; + // remove html from strings if desired + if (!html) { + x = x.replace(htmre, ''); + y = y.replace(htmre, ''); + } // chunk/tokenize - xN = xH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), - yN = yH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + var 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(xH.match(hre), 10) || (xN.length !== 1 && xH.match(dre) && Date.parse(xH)), - yD = parseInt(yH.match(hre), 10) || xD && yH.match(dre) && Date.parse(yH) || null; + xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)), + yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null; // first try and sort Hex codes or Dates if (yD) { @@ -82,11 +94,19 @@ function naturalSort (a, b) { jQuery.extend( jQuery.fn.dataTableExt.oSort, { "natural-asc": function ( a, b ) { - return naturalSort(a,b); + return naturalSort(a,b,true); }, "natural-desc": function ( a, b ) { - return naturalSort(a,b) * -1; + return naturalSort(a,b,true) * -1; + }, + + "natural-nohtml-asc": function( a, b ) { + return naturalSort(a,b,false); + }, + + "natural-nohtml-desc": function( a, b ) { + return naturalSort(a,b,false) * -1; } } ); From 2318bc51c1c55d79624df5c172422a5d29a95fa5 Mon Sep 17 00:00:00 2001 From: Michael Buehler Date: Mon, 4 Jan 2016 15:38:57 +0100 Subject: [PATCH 3/3] Added @author tag --- sorting/natural.js | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/natural.js b/sorting/natural.js index efd6305..6a89380 100644 --- a/sorting/natural.js +++ b/sorting/natural.js @@ -10,6 +10,7 @@ * @name Natural sorting * @summary Sort data with a mix of numbers and letters _naturally_. * @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) + * @author [Michael Buehler] (https://github.com/AnimusMachina) * * @example * $('#example').dataTable( {