Skip to content

Instantly share code, notes, and snippets.

@tristen
Created November 20, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tristen/e79963856608bf54e046 to your computer and use it in GitHub Desktop.
Save tristen/e79963856608bf54e046 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