diff --git a/README.md b/README.md index b767da6..32bbcb0 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ A small & simple sorting component for tables written in Javascript. * numbers * currency * Basic dates in `dd/mm/yy` or `dd-mm-yy` format. Years can be 4 digits. Days and Months can be 1 or 2 digits. +* Dot separated values. E.g. IP addresses or version numbers. ### Additional options diff --git a/src/tablesort.js b/src/tablesort.js index c40a4f5..61a269d 100644 --- a/src/tablesort.js +++ b/src/tablesort.js @@ -117,11 +117,29 @@ return parseDate(bb) - parseDate(aa); }; + var sortDotSep = function(a, b) { + var aa = getInnerText(a.cells[that.col]).split('.'), + bb = getInnerText(b.cells[that.col]).split('.'); + + for (var i = 0, len = aa.length; i < len; i++) { + var aai = parseInt(aa[i]), + bbi = parseInt(bb[i]); + + if (aai == bbi) continue; + if (aai < bbi) return -1; + if (aai > bbi) return 1; + } + return 0; + }; + + // Sort dot separted numbers, e.g. ip addresses or version numbers + if (/^(\d+\.)+\d+$/.test(item)) { + sortFunction = sortDotSep; // Sort as number if a currency key exists or number - if (item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // prefixed currency + } else if (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 - ) { + ) { sortFunction = sortNumber; } else if (testDate(item)) { sortFunction = sortDate;