Skip to content

Commit

Permalink
Merge pull request #55 from ryanmjacobs/dot-sep
Browse files Browse the repository at this point in the history
add another sort type: "dot separated numbers"
  • Loading branch information
tristen committed Jan 10, 2015
2 parents 97251b3 + 9e53552 commit 9b5637b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions src/tablesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9b5637b

Please sign in to comment.