From d51ab642d1db530c2887d0e02caa2cdc2f8c3797 Mon Sep 17 00:00:00 2001 From: davidkonrad Date: Wed, 10 Feb 2016 22:38:59 +0100 Subject: [PATCH] any-number & nepali-numbers sorting plugins --- sorting/any-number.js | 49 ++++++++++++++++++++++++++++++++++ sorting/nepali-numbers.js | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 sorting/any-number.js create mode 100644 sorting/nepali-numbers.js diff --git a/sorting/any-number.js b/sorting/any-number.js new file mode 100644 index 0000000..1980ce3 --- /dev/null +++ b/sorting/any-number.js @@ -0,0 +1,49 @@ +/** + * Sorts columns by any number, ignoring text. This plugin is useful if you have + * mixed content in a column, but still want to sort by numbers. Any number means + * + * - integers, like 42 + * - decimal numbers, like 42.42 / 42,42 + * - signed numbers, like -42.42 / +42.42 + * - scientific numbers, like 42.42e+10 + * - illegal numbers, like 042, which is considered as 42, + * - currency numbers, like €42,00 + * + * Plain text is ignored; columns with no recognizable numerical content + * is pushed to the bottom of the table, both ascending and descending. + * + * @demo http://jsfiddle.net/vkkL5tv7/ + * + * @name Any number + * @summary Sort column with mixed numerical content by number + * @author [david konrad](davidkonrad at gmail com) + * + * @example + * $('#example').dataTable( { + * columnDefs: [ + * { type: 'any-number', targets : 0 } + * ] + * } ); + + * + */ + +_anyNumberSort = function(a, b, high) { + var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; + a = a.replace(',','.').match(reg); + a = a !== null ? parseFloat(a[0]) : high; + b = b.replace(',','.').match(reg); + b = b !== null ? parseFloat(b[0]) : high; + return ((a < b) ? -1 : ((a > b) ? 1 : 0)); +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "any-number-asc": function (a, b) { + return _anyNumberSort(a, b, Number.POSITIVE_INFINITY); + }, + "any-number-desc": function (a, b) { + return _anyNumberSort(a, b, Number.NEGATIVE_INFINITY) * -1; + } +}); + + diff --git a/sorting/nepali-numbers.js b/sorting/nepali-numbers.js new file mode 100644 index 0000000..3b033b0 --- /dev/null +++ b/sorting/nepali-numbers.js @@ -0,0 +1,56 @@ +/** + * Sorts a column containing nepali numbers. Nepali numbers can easily be + * mapped 1:1 to latin numbers - १ = 1, २ = 2, १२ = 12 and so on. + * + * + * + * + * + * + * @name Nepali numbers + * @summary Sorts columns containing UTF8 nepali numbers + * @author [david konrad](davidkonrad at gmail com) + * + * @example + * $('#example').DataTable( { + * columnDefs: [ + * { type: 'nepali-numbers', targets: 0 } + * ] + * } ); + */ + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "nepali-numbers-pre" : function(a) { + function nepaliToLatin(nepali) { + switch(nepali) { + case "०": return 0; break; + case "१": return 1; break; + case "२": return 2; break; + case "३": return 3; break; + case "४": return 4; break; + case "५": return 5; break; + case "६": return 6; break; + case "७": return 7; break; + case "८": return 8; break; + case "९": return 9; break; + default : return 0; break; + } + } + var latin = '', i = 0; + for (i; i b) ? 1 : 0)) + }, + + "nepali-numbers-desc": function(a, b) { + return ((a < b) ? 1 : ((a > b) ? -1 : 0)) + } + +} ); + +