any-number & nepali-numbers sorting plugins

Merge pull request #248 from davidkonrad/master
pull/285/merge
Allan Jardine 8 years ago committed by GitHub
commit 2d638d6d8b

@ -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;
}
});

@ -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.
*
* <https://en.wikipedia.org/wiki/Numbers_in_Nepali_language>
* <http://www.imnepal.com/nepali-numbers>
* <http://stackoverflow.com/q/26856481/1407478>
* <http://jsfiddle.net/ft7f16yt>
*
* @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<a.length; i++) {
latin += nepaliToLatin(a.charAt(i))
}
return parseInt(latin)
},
"nepali-numbers-asc": function(a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0))
},
"nepali-numbers-desc": function(a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0))
}
} );
Loading…
Cancel
Save