-
-
Save mikeymckay/151e61831ba696953749d108ae44edd2 to your computer and use it in GitHub Desktop.
Using tablesort.extend in browserified tablesort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var tablesort = require('tablesort'); | |
function cleanNumber(i) { | |
return i.replace(/[^\-?0-9.]/g, ''); | |
} | |
function compareNumber(a, b) { | |
a = parseFloat(a); | |
b = parseFloat(b); | |
a = isNaN(a) ? 0 : a; | |
b = isNaN(b) ? 0 : b; | |
return a - b; | |
} | |
tablesort.extend('number', function(item) { | |
return item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // Prefixed currency | |
item.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/) || // Suffixed currency | |
item.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/); // Number | |
}, function(a, b) { | |
a = cleanNumber(a); | |
b = cleanNumber(b); | |
return compareNumber(b, a); | |
} | |
); | |
tablesort(document.getElementById('sort')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment