-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.currency.js
32 lines (28 loc) · 823 Bytes
/
sorting.currency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*globals $ */
function cleanCurrency(input) {
'use strict';
var output;
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
output = (input === "-" || input === "--" || input === '' || input.toLowerCase().replace('/', '') === 'na') ? -1 : input.replace(/,/g, "");
/* Remove the currency sign */
if (typeof output === "string" && isNaN(output.substr(0, 1), 10)) {
output = output.substring(1);
}
/* Parse and return */
output = parseFloat(output, 10);
return output;
}
$.extend( $.fn.dataTableExt.oSort, {
"currency-asc" : function (a, b) {
'use strict';
var x = cleanCurrency(a);
var y = cleanCurrency(b);
return x - y;
},
"currency-desc" : function (a, b) {
'use strict';
var x = cleanCurrency(a);
var y = cleanCurrency(b);
return y - x;
}
});