tablesort.numeric.js 673 B

1234567891011121314151617181920212223242526
  1. (function(){
  2. var cleanNumber = function(i) {
  3. return i.replace(/[^\-?0-9.]/g, '');
  4. },
  5. compareNumber = function(a, b) {
  6. a = parseFloat(a);
  7. b = parseFloat(b);
  8. a = isNaN(a) ? 0 : a;
  9. b = isNaN(b) ? 0 : b;
  10. return a - b;
  11. };
  12. Tablesort.extend('number', function(item) {
  13. return item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // Prefixed currency
  14. item.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/) || // Suffixed currency
  15. item.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/); // Number
  16. }, function(a, b) {
  17. a = cleanNumber(a);
  18. b = cleanNumber(b);
  19. return compareNumber(b, a);
  20. });
  21. }());