Skip to content

Instantly share code, notes, and snippets.

@mikeymckay
Forked from tristen/index.js
Last active May 11, 2016 10:58
Show Gist options
  • Save mikeymckay/151e61831ba696953749d108ae44edd2 to your computer and use it in GitHub Desktop.
Save mikeymckay/151e61831ba696953749d108ae44edd2 to your computer and use it in GitHub Desktop.
Using tablesort.extend in browserified tablesort
'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