Sorting - natural: Updated to use `localeCompare`

https://datatables.net/forums/discussion/comment/228331
pull/583/head
Allan Jardine 9 months ago
parent 8939a585fb
commit d9592b38b4

@ -1,151 +1,108 @@
/*! © SpryMedia Ltd, Jim Palmer, Michael Buehler, Mike Grier, Clint Priest, Kyle Adams, guillermo - datatables.net/license */ /*! © SpryMedia Ltd - datatables.net/license */
/** /**
* Data can often be a complicated mix of numbers and letters (file names * Data can often be a complicated mix of numbers and letters (file names
* are a common example) and sorting them in a natural manner is quite a * are a common example) and sorting them in a natural manner is quite a
* difficult problem. * difficult problem.
* *
* Fortunately a deal of work has already been done in this area by other * Fortunately the Javascript `localeCompare` method is now widely supported
* authors - the following plug-in uses the [naturalSort() function by Jim * and provides a natural sorting method we can use with DataTables.
* Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables.
* *
* @name Natural sorting * @name Natural sorting
* @summary Sort data with a mix of numbers and letters _naturally_. * @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 Allan Jardine
* @author [Michael Buehler] (https://github.com/AnimusMachina) * @requires DataTables 2+
* *
* @example * @example
* $('#example').dataTable( { * // Natural sorting
* new DataTable('#myTable',
* columnDefs: [ * columnDefs: [
* { type: 'natural', targets: 0 } * { type: 'natural', target: 0 }
* ] * ]
* } ); * } );
* *
* Html can be stripped from sorting by using 'natural-nohtml' such as * @example
* // Html can be stripped from sorting by using 'natural-nohtml' such as
* new DataTable('#myTable',
* columnDefs: [
* { type: 'natural-nohtml', target: 0 }
* ]
* } );
* *
* $('#example').dataTable( { * @example
* // Case insensitive natural sorting
* new DataTable('#myTable',
* columnDefs: [ * columnDefs: [
* { type: 'natural-nohtml', targets: 0 } * { type: 'natural-ci', target: 0 }
* ] * ]
* } ); * } );
* *
*/ */
import DataTable from 'datatables.net'; import DataTable from "datatables.net";
/* DataTable.type("natural", {
* Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license order: {
* Author: Jim Palmer (based on chunking idea from Dave Koelle) asc: function (a, b) {
* Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo return a.localeCompare(b, navigator.languages[0] || navigator.language, {
* See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js numeric: true,
*/ ignorePunctuation: true,
function naturalSort(a, b, html) { });
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?%?$|^0x[0-9a-f]+$|[0-9]+)/gi, },
sre = /(^[ ]*|[ ]*$)/g, desc: function (a, b) {
dre = return (
/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) *
hre = /^0x[0-9a-f]+$/i, -1
ore = /^0/, );
htmre = /(<([^>]+)>)/gi, },
// convert all to strings and trim() },
x = a.toString().replace(sre, '') || '', className: "natural-sort",
y = b.toString().replace(sre, '') || ''; });
// remove html from strings if desired DataTable.type("natural-nohtml", {
if (!html) { order: {
x = x.replace(htmre, ''); asc: function (a, b) {
y = y.replace(htmre, ''); a = DataTable.util.stripHtml(a);
} b = DataTable.util.stripHtml(b);
// chunk/tokenize
var xN = x return a.localeCompare(b, navigator.languages[0] || navigator.language, {
.replace(re, '\0$1\0') numeric: true,
.replace(/\0$/, '') ignorePunctuation: true,
.replace(/^\0/, '') });
.split('\0'), },
yN = y desc: function (a, b) {
.replace(re, '\0$1\0') a = DataTable.util.stripHtml(a);
.replace(/\0$/, '') b = DataTable.util.stripHtml(b);
.replace(/^\0/, '')
.split('\0'), return (
// numeric, hex or date detection a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) *
xD = -1
parseInt(x.match(hre), 10) || );
(xN.length !== 1 && x.match(dre) && Date.parse(x)), },
yD = },
parseInt(y.match(hre), 10) || className: "natural-sort",
(xD && y.match(dre) && Date.parse(y)) || });
null;
DataTable.type("natural-ci", {
// first try and sort Hex codes or Dates order: {
if (yD) { asc: function (a, b) {
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)
var oFxNcL =
(!(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc])) || xN[cLoc] || 0;
var 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;
}
DataTable.ext.type.order['natural-asc'] = function (a, b) {
return naturalSort(a, b, true);
};
DataTable.ext.type.order['natural-desc'] = function (a, b) {
return naturalSort(a, b, true) * -1;
};
DataTable.ext.type.order['natural-nohtml-asc'] = function (a, b) {
return naturalSort(a, b, false);
};
DataTable.ext.type.order['natural-nohtml-desc'] = function (a, b) {
return naturalSort(a, b, false) * -1;
};
DataTable.ext.type.order['natural-ci-asc'] = function (a, b) {
a = a.toString().toLowerCase(); a = a.toString().toLowerCase();
b = b.toString().toLowerCase(); b = b.toString().toLowerCase();
return naturalSort(a, b, true); return a.localeCompare(b, navigator.languages[0] || navigator.language, {
}; numeric: true,
ignorePunctuation: true,
DataTable.ext.type.order['natural-ci-desc'] = function (a, b) { });
},
desc: function (a, b) {
a = a.toString().toLowerCase(); a = a.toString().toLowerCase();
b = b.toString().toLowerCase(); b = b.toString().toLowerCase();
return naturalSort(a, b, true) * -1; return (
}; a.localeCompare(b, navigator.languages[0] || navigator.language, { numeric: true, ignorePunctuation: true }) *
-1
);
},
},
className: "natural-sort",
});

Loading…
Cancel
Save